传递远程Powershell变量

时间:2015-12-31 15:27:07

标签: powershell variables remote-desktop

我正在尝试编写简单的脚本来提高我的PowerShell能力,而且我认为我经常做的事情就是通过服务器管理器或远程PowerShell会话来阻止我们的RDS服务器应用更新等。

我尝试根据输入编写脚本来执行此操作,但无论我做什么,变量的内容都不会传递到远程服务器上。

该脚本只询问您希望更改哪个服务器,您希望将其更改为哪个状态,将两者的值组合在一起,然后将其传递到远程服务器上。

唉...没什么。代码如下。任何指针都将非常感激。

$SessionHosts = "Server_1", "Server_2", "Server_3", "Server_4" 

$OpeningSalvo = Read-Host -Prompt "[Q]uery Servers or [C]hange State?"

If ($OpeningSalvo -match "q")

    {   
         Invoke-Command $SessionHosts {chglogon.exe /query}

    }

Elseif ($OpeningSalvo -match "c")

    {

        $Server = Read-Host -Prompt "Server Name?"    

        $State = Read-Host -Prompt "Enable, Drain, Drainuntilrestart or Disable"

        $Run = "chglogon.exe /" + "$State"

        #Invoke-Command -ComputerName $Server -ScriptBlock {Write-Host $Fun $using:Run echo $Fun}

        #$ScriptBlockContent1 = {param ($Run) Write-Host $Run} 
        #Invoke-Command $Server $ScriptBlockContent1 -ArgumentList $Run

        $ScriptBlockContent1 = {param ($Run) Write-Output $Run} 
        Invoke-Command $Server $ScriptBlockContent1 -ArgumentList $Run

        $ScriptBlockContent2 = {Write-Host $Run}
        Invoke-Command $Server $ScriptBlockContent2

    }

Else  

    {
        Write-Host "End"
    }

我已经设法使用以下powershell脚本来实现这一点,但是,它有点笨拙!

$SessionHosts = "Server_1", "Server_2", "Server_3", "Server_4" 

$OpeningSalvo = Read-Host -Prompt "[C]hange State, [Q]uery Servers or [E]xit?"

If ($OpeningSalvo -match "q")

    {   
         Invoke-Command $SessionHosts {chglogon.exe /query}

    }

Elseif ($OpeningSalvo -match "c")

    {

        $Server = Read-Host -Prompt "Server Name?"    

        $State = Read-Host -Prompt "[1] Enable, [2] Drain, [3] Drain until restart, [4] Disable all connections"

        If ($State -match "1") 
                {Invoke-Command $Server {chglogon.exe /enable}}

        Elseif ($State -match "2")
                {Invoke-Command $Server {chglogon.exe /drain}}

        Elseif ($State -match "3")
                {Invoke-Command $Server {chglogon.exe /drainuntilrestart}}

        Elseif ($State -match "4")
                {Invoke-Command $Server {chglogon.exe /disable}}


    }

Else  

    {
        Write-Host "End"
    }

顺便说一句,第一个实例中chglogon.exe / query的输出有点长。

是否有任何建议将此管道传输到内存中,然后拉出服务器名称和状态?我确信我可以很好地格式化它。

事实上,任何建议都将受到赞赏!这个脚本变得越复杂和有用,我就会学得越多!可能有错误,但这是我的第一个脚本! :)

“会话登录目前已启用     + CategoryInfo:NotSpecified :(会话登录当前已启用:字符串)[],RemoteException     + FullyQualifiedErrorId:NativeCommandError     + PSComputerName:Server2“

2 个答案:

答案 0 :(得分:1)

如果您没有深入了解问题的具体细节,您似乎已经遇到了在脚本块中扩展外部变量的问题。

如果是这样,解决方案并不是很漂亮。我不久前遇到了这个问题,解决方法是首先将脚本生成为字符串。这样,变量将被扩展。然后,使用ScriptBlock静态方法Create()创建一个脚本块,扩展您的内容。

此博客 - http://blogs.technet.com/b/heyscriptingguy/archive/2013/05/22/variable-substitution-in-a-powershell-script-block.aspx - 以一个例子给出了解释:

PS C:\> $a = "This is a string"
PS C:\> $b = "The value of `$a is $a"
PS C:\> $myScriptBlock = [ScriptBlock]::Create($b)

答案 1 :(得分:1)

blog开始,关于如何用powershell替换chglogon.exe:

# Query
gwmi win32_terminalservicesetting -N "rootcimv2terminalservices" | %{
    if ($_.logons -eq 1){"Disabled"}
    else {
        switch ($_.sessionbrokerdrainmode)
        {
            0 {"Enabled"}
            1 {"DrainUntilRestart"}
            2 {"Drain"}
            default {"something’s not right here!"}
        }
    }
}

# Change logon /Enable
$temp = (gwmi win32_terminalservicesetting -N "rootcimv2terminalservices")
$temp.sessionbrokerdrainmode=0
$temp.logons=0
$temp.put()

# Change Logon /Disable
$temp = (gwmi win32_terminalservicesetting -N "rootcimv2terminalservices")
$temp.logons=1
$temp.put()

# Change Logon /Drain
$temp = (gwmi win32_terminalservicesetting -N "rootcimv2terminalservices")
$temp.sessionbrokerdrainmode=2
$temp.put()

# Change Logon /DrainUntilRestart
$temp = (gwmi win32_terminalservicesetting -N "rootcimv2terminalservices")
$temp.sessionbrokerdrainmode=1
$temp.put()

将此与-Computer gwmi上的rawdat.plot(x='ts', y=['bid','ask'], marker='.', lw=0) plot.hold() rawdat.plot(x='lastTrade', y='last', marker='x', lw=0) plt.show() 参数相结合,您可以更换chglogon。