StarWind iSCSI SAN
StarWind Native SAN for Hyper-V
 

Powershell cmdlets: running a script remotely

Software-based VM-centric and flash-friendly VM storage + free version, StarWind VSAN "Ready Nodes"

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

Powershell cmdlets: running a script remotely

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

Re: Powershell cmdlets: running a script remotely

Postby Boris (staff) » Fri Jan 11, 2019 4:13 pm

powershell.exe -sta -file "path_to_your_script.ps1" - did you try it this way?
Boris (staff)
Staff
 
Posts: 805
Joined: Fri Jul 28, 2017 8:18 am

Re: Powershell cmdlets: running a script remotely

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

Re: Powershell cmdlets: running a script remotely

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

Re: Powershell cmdlets: running a script remotely

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

Re: Powershell cmdlets: running a script remotely

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


Return to StarWind Virtual SAN (VSAN) & VSAN Free, StarWind HyperConverged Appliance (HCA) & Storage Appliance (SA)

Who is online

Users browsing this forum: No registered users and 7 guests