Page 1 of 1

Powershell cmdlets: running a script remotely

Posted: Fri Jan 11, 2019 2:35 pm
by cforest
Hi there,

Just wanted to ask if it is somehow possible to use StarWindX cmdlets while running a PowerShell script on a remote computer. Here is my code:

Code: Select all

param (
    $tapeLibMachine
)

$mycreds = ConvertTo-SecureString "MyPassword" -AsPlainText -Force | % {New-Object System.Management.Automation.PSCredential ("$tapeLibMachine\Administrator", $_)}

$scriptBlock = {

    try {
       
        Import-Module StarWindX

        $server = New-SWServer -host 127.0.0.1 -port 3261 -user root -password starwind

        Enable-SWXLog

        $server.Connect()
	
    }
    catch {
        $errDescription = $Error[0].Exception.Message
        "Error occured: $errDescription"

    }
    finally {
        $server.Disconnect()
    }

}


Invoke-Command -Computername $tapeLibMachine -Credential $mycreds -ScriptBlock $scriptBlock  
Here is the output:

Code: Select all

C:\Scripts\>powershell .\test.ps1 avo-vtl
StarWindX doesn't support current appartment. You need to run this script with -STA switch to use StarWindX.
At C:\Scripts\test.ps1:32 char:1
+ Invoke-Command -Computername $tapeLibMachine -Credential $mycreds -ScriptBlock $ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (StarWindX doesn... use StarWindX.:String) [], RuntimeException
    + FullyQualifiedErrorId : StarWindX doesn't support current appartment. You need to run this script with -STA switch to use StarWindX.
I am able to use StarWindX cmdlets locally and fail to understand what I miss when using them remotely. On both remote and local machines, Powershell 4 is installed, and STA mode is the default. I even tried to use -sta in the command line (powershell -sta -file test.ps1 avo-vtl), but it didn't help.

Any chances to make it work?

Thank you.

Re: Powershell cmdlets: running a script remotely

Posted: Fri Jan 11, 2019 4:13 pm
by Boris (staff)
powershell.exe -sta -file "path_to_your_script.ps1" - did you try it this way?

Re: Powershell cmdlets: running a script remotely

Posted: Fri Jan 11, 2019 4:42 pm
by cforest
Boris (staff) wrote:powershell.exe -sta -file "path_to_your_script.ps1" - did you try it this way?
I did. It doesn't work, the same error.

Re: Powershell cmdlets: running a script remotely

Posted: Fri Jan 11, 2019 6:59 pm
by Boris (staff)
Can you give me an example of how you ran it with STA enabled?

I would try saving this script on the remote server and do something like

Code: Select all

Invoke-Command -Computername $tapeLibMachine -Credential $mycreds -ScriptBlock {powershell.exe -STA -file C:\myFolder\myScript.ps1}

Re: Powershell cmdlets: running a script remotely

Posted: Mon Jan 14, 2019 9:43 am
by cforest
Boris, thank you so much, it works!

Honestly, running a script inside a script is a bit less convenient than specifying a code inside a script block, but I will be able to cope with that.

Thanks!

Re: Powershell cmdlets: running a script remotely

Posted: Mon Jan 14, 2019 9:52 am
by anton (staff)
Great!

P.S. I've moved (+shadow) thread to StarWind forum if you don't mind...