-
PowerShell Tip – Convert Script Block to string or string to Script Block
It is very simple. You just need to create a new instance of System.Management.Automation.ScriptBlock and use Create() method. PowerShell code $scriptBlock1 = { Get-ChildItem } Write-Verbose -Message $scriptBlock1.GetType().FullName -Verbose $scriptBlockString = $scriptBlock.ToString() Write-Verbose -Message $scriptBlockString.GetType().FullName -Verbose $scriptBlock2 = [ScriptBlock]::Create($scriptBlockString) Write-Verbose -Message $scriptBlock2.GetType().FullName -Verbose # Execute& $scriptBlock2 Output VERBOSE: System.Management.Automation.ScriptBlock VERBOSE: System.String VERBOSE: System.Management.Automation.ScriptBlock
-
How to copy values of variables into PowerShell Script Block and keep it intact (remember it)?
There are situations where you need to create a script block, save it into variable and then execute it once or multiple times. Simply said, [ScriptBlock] is just an anonymous function so if you do this: $scriptBlock = { $someVar } & $scriptBlock Then you will get someVar variable from the current scope or from the…
-
PowerShell Tip – How to set permissions that applies to folder, subfolder and files without iCacls?
It is very simple… $path = ‘C:\Temp’ New-Item -Path $path -ItemType directory $acl = Get-Acl -Path $path $permission = ‘Everyone’, ‘FullControl’, ‘ContainerInherit, ObjectInherit’, ‘None’, ‘Allow’ $rule = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule -ArgumentList $permission $acl.SetAccessRule($rule) $acl | Set-Acl -Path $path
-
PowerShell advanced function (cmdlet) – Report all VMs with disconnected NICs in the specified Virtual Machine Manager
This is very simple function to report all virtual machines in the defined System Center Virtual Machine Manager (SCVMM) with disconnected network adapters or with network adapters without specified VLAN. Examples Report Get-RvSCVirtualNetworkAdapter -VMMServer firstVMM, secondVMM, thirdVMM -NoVMNetworkOrNoVLan | Out-GridView Code Function Get-RvSCVirtualNetworkAdapter { <# .SYNOPSIS Get network adapters of virtual machine on defined SCVMM…
-
PowerShell Tip – How to check number or cores, logical processors and Hyper-threading on multiple hosts?
It is very simple… Get-CimInstance -ClassName Win32_Processor -CimSession host1, host2 | Format-Table PSComputerName, NumberOfCores, NumberOfLogicalProcessors