Powershell cmdlets: running a script remotely

Software-based VM-centric and flash-friendly VM storage + free version

Moderators: anton (staff), art (staff), Max (staff), Anatoly (staff)

Post Reply
cforest
Posts: 15
Joined: Fri Dec 14, 2018 10:48 am

Fri Jan 11, 2019 2:35 pm

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.
Boris (staff)
Staff
Posts: 805
Joined: Fri Jul 28, 2017 8:18 am

Fri Jan 11, 2019 4:13 pm

powershell.exe -sta -file "path_to_your_script.ps1" - did you try it this way?
cforest
Posts: 15
Joined: Fri Dec 14, 2018 10:48 am

Fri Jan 11, 2019 4:42 pm

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.
Boris (staff)
Staff
Posts: 805
Joined: Fri Jul 28, 2017 8:18 am

Fri Jan 11, 2019 6:59 pm

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}
cforest
Posts: 15
Joined: Fri Dec 14, 2018 10:48 am

Mon Jan 14, 2019 9:43 am

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!
User avatar
anton (staff)
Site Admin
Posts: 4008
Joined: Fri Jun 18, 2004 12:03 am
Location: British Virgin Islands
Contact:

Mon Jan 14, 2019 9:52 am

Great!

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

Chief Technology Officer & Chief Architect, StarWind Software

Image
Post Reply