Translate

PowerShell Playbook

This is my private PowerShell playbook. (Update 07/2020)

REMARK: Using the new blogger UI updating this page corrupted it! So do not rely on these informations in the moment. I am in contact with Google to get this fixed!
Some sort of unstructured but very helpful hints to fix PowerShell related Stuff. Some stuff I found on other places in the web and it just helps me when creating my own scripts.


Tips and Tricks

1. Case of the DoubleQuote (When you need to pass variables containing spaces or other strange characters)
$doublequote
Example:

2. Get to know if my process is running in 64bi
$PSenvIs64bit


3. For testing on a 64bit machine you want to test the setup with the setup environment of SCCM client which is 32bit.
 %windir%\SysWoW64\cmd.exe

4. Excecute a 64bit Powershell from a 32bit process (SCCM Client)

%windir%\Sysnative\windowsPowershell\V1.0\PowerShell.exe -NoProfile -ExecutionPolicy Bypass -file %~dp0MyScript.ps1

5. Execute a self elevating Powershell Script
https://blogs.msdn.microsoft.com/virtual_pc_guy/2010/09/23/a-self-elevating-powershell-script/



6a. SystemAccount (SCCM Client) modifies all users profile (RegKeys)
    (Load Hive Keys WAY runs before loading)
http://www.adminarsenal.com/admin-arsenal-blog/modifying-the-registry-users-powershell/

6b. SystemAccount (SCCM Client) modifies all users profile (RegKeys)
    (More Elegant WAY runs with Active Setup Part)
https://gallery.technet.microsoft.com/scriptcenter/Easily-set-a-registry-b3449784
NOTE on Win10 need to be fixed (Double Active Setup Entries in the same root!!! This is like pregnant fathers. Impossible but true!!!) Seems to be a BUG in WINDOWS 10 !!!
Also noted that MS recently mentioned that Active Setup should not be used for other 3rd Party setups!!!!


7. Easy way to implement PowerShell Code Signing
http://www.darkoperator.com/blog/2013/3/5/powershell-basics-execution-policy-part-1.html

8. Figure out the OS Language with .Net in Powershell
$SystemLanguage =[CultureInfo]::InstalledUICulture
may result e.g. in en-US or de-DE or other combinations.

9. To get out if having VPN connections (MS Builtin VPNs)
MS VPN:
Get-VpnConnection | Select Name, ConnectionStatus

MS DirectAccess:
Get-DAConnectionStatus

3rd Party VPNs detect:
https://gallery.technet.microsoft.com/scriptcenter/Test-VPNConnection-Check-36fa4b57

10. Differences between 10 ways of starting an executable or external process
https://social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx


11. Call a Win32 API from Powershell (Example Code)


$MethodDefinition = @’
[DllImport(“kernel32.dll”, CharSet = CharSet.Unicode)]
public static extern bool
‘@
$Kernel32 = Add-Type -MemberDefinition $MethodDefinition -Name ‘Kernel32’ -Namespace ‘Win32’ -PassThru

# You may now call for example the CopyFile function with its
# arguments in brackets. For arguments check please API
# documentation on docs.microsoft.com. Which you also need for
# the argument definition above which is also highlighted!
# Copy calc.exe to the user’s desktop

$Kernel32

12. Clever handling with services. 

Better to use .NET integrated functions directly from PowerShell.
In this example we stop a service, wait for finishing before we continue. The trick is the .WaitForStatus. This eleminates the need for a wile loop.

$ccmexecservice = [System.ServiceProcess.ServiceController]::new('ccmexec')

TOOLS

A. Best extension ever for Powershell ISE "iseSteroids"
http://www.powertheshell.com/isesteroids/