如果我运行下面的代码,则创建的作业“ test3”始终处于“已阻止”状态。它永远不会执行。它没有其他错误消息。 当我分别在start-job脚本块中运行命令时,它确实起作用(restart-computer-计算机名$ computer -credential $ MySecureCreds -force)。
[void]
[xml]$XAML = @"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="CSS Server Reboot" Height="450" Width="800">
<Grid>
<ComboBox Name="Server_Combobox" SelectedValuePath="Content"
HorizontalAlignment="Left" Margin="240,107,0,0" VerticalAlignment="Top"
Width="120">
<ComboBoxItem Name="Server1">10.15.12.148</ComboBoxItem>
</ComboBox>
<Label Name="Title_Label" Content="CSS – Server Reboot
"
HorizontalAlignment="Left" Margin="240,41,0,0" VerticalAlignment="Top"
Height="34" Width="284" FontSize="16"/>
<Button Name="Reboot_Button" Content="Reboot" HorizontalAlignment="Left"
Margin="449,107,0,0" VerticalAlignment="Top" Width="75"/>
<TextBox Name="Reboot_Textbox" Grid.Column="1" HorizontalAlignment="Left"
Height="173" Margin="81,180,0,0" TextWrapping="Wrap"
VerticalScrollBarVisibility="Auto" Text="" VerticalAlignment="Top"
Width="294"/>
</Grid>
</Window>
"@
#Read XAML
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
try{$Form=[Windows.Markup.XamlReader]::Load( $reader )}
catch{Write-Host "Unable to load Windows.Markup.XamlReader"; exit}
# Store Form Objects In PowerShell
$xaml.SelectNodes("//*[@Name]") | ForEach-Object {Set-Variable -Name
($_.Name) -Value $Form.FindName($_.Name)}
# Secure credential creation
$Username = "XXX"
$Password = "XXX"
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$SecureString = $pass
$MySecureCreds = New-Object -TypeName
System.Management.Automation.PSCredential -ArgumentList $Username,$pass
# Use the combobox
$computer
$jobtest
$timer = new-object System.Windows.Threading.DispatcherTimer
$Server_Combobox.add_SelectionChanged( {
param($sender, $args)
$Script:computer = $sender.SelectedItem.Content
} )
#Assign reboot event to button
$Reboot_Button.Add_Click({
$sb = [scriptblock]::Create("restart-computer -computername $computer -
credential $MySecureCreds -force")
Start-Job -Name "test3" -ScriptBlock $sb
$Script:jobtest = Get-Job -Name "test3"
$Reboot_Textbox.AddText("The restart procedure is starting.`n")
$timer.Interval = [TimeSpan]"0:0:5.00"
$timer.add_Tick({
if($jobtest.State -ne "Running"){
$Reboot_Textbox.AddText("The restart failed, please contact IT Ops !!!`n")
$Reboot_Textbox.AddText("The error message is:`n")
$Reboot_Textbox.AddText($jobtest.ChildJobs[0].JobStateInfo.State)
$Reboot_Textbox.AddText("`n")
$Reboot_Textbox.AddText($jobtest.ChildJobs[0].JobStateInfo)
$Reboot_Textbox.AddText("`n")
$Reboot_Textbox.AddText($jobtest.ChildJobs[0].Error)
$Script:timer.Stop()
}elseif($jobtest.State -eq "Completed"){
$Reboot_Textbox.AddText("The restart procedure completed successfully.")
$Script:timer.Stop()
Wait-Job -Name test3 | Remove-Job
}else{
$Reboot_Textbox.AddText("The restart procedure is ")
$Reboot_Textbox.AddText($jobtest.State)
$Reboot_Textbox.AddText("`n")
}
})
$timer.Start()
})
$Form.ShowDialog() | out-null
有人知道如何解决吗?它与脚本块中的“凭据”选项有关吗?
答案 0 :(得分:1)
更改此:
$sb = [scriptblock]::Create("restart-computer -computername $computer -credential $MySecureCreds -force")
对此:
$sb = { restart-computer -computername $Using:computer -credential $Using:MySecureCreds -force }
您需要Using:
范围修饰符,以便将当前值注入到脚本块中。
当您使用双引号字符串进行第一种方法时,变量将在创建脚本块之前放入字符串中。
您最终得到的东西是:
$sb = { restart-computer -computername WhateverIsInComputer -credential System.Management.Automation.PSCredential -force }
哪个不行。