Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dan1el42/9b9fd41ee2bb541eaae6cae7bfb007b4 to your computer and use it in GitHub Desktop.
Save Dan1el42/9b9fd41ee2bb541eaae6cae7bfb007b4 to your computer and use it in GitHub Desktop.
#Requires -Version 3.0
function Invoke-CreateNewVM
{
param(
[Parameter(Mandatory)]
[Alias('HostName')]
[String]
$ComputerName,
[Parameter(Mandatory)]
[String]
$Param1,
[Parameter(Mandatory)]
[String]
$Param2,
[Parameter(Mandatory)]
[String]
$Param3,
[Parameter(Mandatory)]
[Int]
$Param4
)
$ScriptBlock = {
function Get-Xyz { 'MOTD' }
# Create local variables
$Param1 = $Using:Param1
$Param2 = $Using:Param2
$Param3 = $Using:Param3
$Param4 = $Using:Param4
# Showing the usage of the variables
"> Param1: $Param1"
"> Param2: ${Param2}"
'> Param3: {0}' -f $Param3
'> Param4: {0}' -f $Using:Param4
# do a bunch of stuff on my remote machine
$var = Get-Xyz
$htmlBlock = @"
Whether i do $var or ${var} here, either way, when i try to expand it later, it is empty. What am i doing wrong?
"@
# I try to now expand $htmlBlock in a Send-MailMessage command, but $var is blank, empty.. ??
Send-Mail-Message -body $htmlBlock
}
$PSSession = New-PSSession -ComputerName $ComputerName
try {
Invoke-Command -Session $PSSession -ScriptBlock $ScriptBlock
} finally {
Remove-PSSession -Session $PSSession -Confirm:$false
}
}
$Params = @{
ComputerName = 'vm01'
Param1 = 'US West'
Param2 = 'example.com'
Param3 = 'Sales'
Param4 = 'Jane Doe'
}
Invoke-CreateNewVM @Params
# Alternative notation which unfortunately can get very long with additional parameters
#Invoke-CreateNewVM -ComputerName 'vm01' -Param1 'US West' -Param2 'example.com' -Param3 'Sales' -Param4 'Jane Doe'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment