Я хочу использовать переменную powershell в текстовом блоке в WPF. Как я могу это сделать?
Add-Type -AssemblyName PresentationFramework
$RegWUAURebootReq = Get-ChildItem "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\" | Select -ExpandProperty Name | Split-Path -Leaf
$WUAURebootReq = $RegWUAURebootReq -contains "RebootRequired"
$today = Get-Date
$lastupdateinstalleddate = Get-WmiObject -Class "win32_quickfixengineering" | Select -ExpandProperty InstalledOn | Sort-Object -Descending | select -First 1
$elapsedtime = New-TimeSpan -Start $lastupdateinstalleddate -End $today | Select TotalDays | Select -ExpandProperty TotalDays
if($WUAURebootReq -or ($elapsedtime -le 3))
{
[xml]$Form = @'
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Title="Pending Restart" Height="234.574" Width="530.319" Background="#E3EDF1" WindowStartupLocation="CenterScreen">
<Grid>
<Button Name="Restart" Content="Restart now" HorizontalAlignment="Left" Margin="263,150,0,0" VerticalAlignment="Top" Width="100" Background="#000000" Foreground="#FFFFFF"/>
<Button Name="Postpone" Content="Remind me later" HorizontalAlignment="Left" Margin="382,150,0,0" VerticalAlignment="Top" Width="100" Background="#000000" Foreground="#FFFFFF"/>
<TextBlock HorizontalAlignment="Left" Margin="28,30,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="106" Width="463">
<Run FontWeight="Bold" Text="Pending Restart!"/><LineBreak/><Run Text="Last update on this computer was installed on $lastupdateinstalleddate."/>
<LineBreak/><Run/><LineBreak/><Run Text="It's detected that restart is still pending on this computer."/><LineBreak/>
<Run Text="Kindly restart this computer as soon as possible to make your computer compliant with the updates."/></TextBlock>
</Grid>
</Window>
'@
$NR = (New-Object System.Xml.XmlNodeReader $Form)
$Win = [Windows.Markup.XamlReader]::Load( $NR )
$Restart = $win.FindName("Restart")
$Postpone = $win.FindName("Postpone")
$Restart.Add_Click({
#Some code
})
$Postpone.Add_Click({
Exit
})
$Win.WindowStyle = "SingleBorderWindow"
$Win.ShowDialog()
}
else
{
Exit
}
Я не могу использовать $ lastupdateinstalleddate в TextBlock.
Спасибо всем за ваши предложения. Я отправил полный код сейчас.
Мне все еще трудно использовать переменную здесь.