pf-script.ps1
function New-ScriptBlock ($script, [string[]]$modules = @(), $snapins) { if ($snapins) { $registered = ( Get-PSSnapin -Registered ).Name $snapinList = Get-PSSnapin | Where-Object name -in $registered | Where-Object name -like $snapins | ForEach-Object { "Add-PSSnapin $($_.Name)" } } if ( $modules ) { $ipmo = @() foreach ($module in $modules) { $ipmo += Get-Module | Where-Object name -like $module } $ipmo = $ipmo | Select-Object -Unique $ipmoScript = $ipmo | ForEach-Object { "Import-Module $($_.Path) -DisableNameChecking" } } $inst = @() + $snapinList + $ipmoScript + $script $batch = $inst -join "`n" $result = [ScriptBlock]::Create( $batch ) return $result } |