Allow variable names to be displayed in the toast notification
Planning to use this for when a specific account logs into a specific computer. Using Task scheduler to fire off the following .ps1 script:
$PCName=(Get-CimInstance -ClassName Win32_ComputerSystem).Name $CurrentDate=Get-Date Invoke-Command -ComputerName <Remote Computer> -ScriptBlock {New-BurntToastNotification -Text "SPECIFIED ACCOUNT HAS LOGGED IN TO $PCName ON $CurrentDate" -Sound 'Alarm2'}
Currently the variables are missing when the toast notification comes up.
Thanks :)
What you're running into there is a scoping issue. When running Invoke-Command the ScriptBlock runs in an environment where those variables do not exist.
The "using" scope should fix this, e.g. "SPECIFIED ACCOUNT HAS LOGGED IN TO $using:PCName ON $using:CurrentDate"
Check out this post for more info: https://powershellexplained.com/2016-08-28-PowerShell-variables-to-remote-commands/