All PowerPacks scripts must end with Exit-PSScript $Error or they will fail.
Exit-PSScript $Error
How to suppress recording any errors when using PowerShell commands (if supported).
Get-Process -Name 'dummy' -ErrorAction:Ignore
Create Functions - it is recommended to use the “approved” naming convention when creating functions. (Verb-Noun)
# get list of approved verbs Get-Verb # simple function example function Start-InitializeSDK { $script:cms=New-Object -ComObject "CapaInstaller.SDK" $script:cms.SetDatabaseSettings($global:databaseserver,$global:database,$false) Write-Host $script:cms.GetDllVersion() $script:cms.SetDefaultManagementPoint($global:cmpid) $script:cms.GetManagementPoints() $script:cms.SetSplitter("|") }
When calling Functions never use “( )” parentheses.
# Correct Start-InitializeSDK # Wrong Start-InitializeSDK()
How to encrypt passwords.
[CmdletBinding()] param ( [Parameter(Mandatory=$true)] [string]$Password ) if (!$Password.Trim()){Write-Error "You must type a password to encrypt" -ErrorAction Stop} #generate random AES key using 256-bit $Key = New-Object Byte[] 32 [Security.Cryptography.RNGCryptoServiceProvider]::Create().GetBytes($Key) $pw = ConvertTo-SecureString -String $Password -AsPlainText -Force $encPassword=ConvertFrom-SecureString -SecureString $pw -Key $Key Clear-Host Write-Host "Encrypted password:`r`n$encPassword" Write-Host Write-Host "AES key:`r`n$($key -join ',')" Write-Host
Join domain.
[CmdletBinding()] Param( [Parameter(Mandatory=$true)] [string]$Packageroot, [Parameter(Mandatory=$true)] [string]$AppName, [Parameter(Mandatory=$true)] [string]$AppRelease, [Parameter(Mandatory=$true)] [string]$LogFile, [Parameter(Mandatory=$true)] [string]$TempFolder, [Parameter(Mandatory=$true)] [string]$DllPath, [Parameter(Mandatory=$false)] [Object]$InputObject=$null ) try { [string]$global:username='internal\userjoin' [string]$global:domain='internal.mydomain.dk' [string]$global:aeskey=@(159,185,19,134,62,241,80,22,70,244,23,168,237,185,210,199,58,255,173,93,250,159,101,244,247,44,211,43,100,217,26,25) [string]$global:encpass='25892d1116743f0423413b16050a5345MgB8AGYAWgAwAGoAdAB1AHoANQBzAHUANgAwAHMAOQBWAEgAYgBSAG4AbwBsAFEAPQA9AHwAMAA2AGUAOQBjADkAZAAyAGYANQBkAGUAMAA3ADEAMgA0ADQAMAAzAGYAYQBjAGYAOAA5AGUAOQBjAGEAMQBmAGUANwA3AGQANABmADQAYQAwADQAZgA4ADcAYQA5AGMAMAAxADUANQAwAGMAYQA5ADkAZABhAGIAZAAzAGUANAA=' [string]$global:ou='OU=Windows 10,OU=Workstations,DC=internal,DC=mydomain,DC=dk' ### Download package kit [bool]$global:DownloadPackage = $false ############################################## #load core PS lib - don't mess with this! if ($InputObject){$pgkit=""}else{$pgkit="kit"} Import-Module (Join-Path $Packageroot $pgkit "PSlib.psm1") -ErrorAction stop #load Library dll $cs=Add-PSDll ############################################## #Begin $cs.Job_Start("WS",$AppName,$AppRelease,$LogFile,"INSTALL") $cs.Job_WriteLog("[Init]: Starting package: '" + $AppName + "' Release: '" + $AppRelease + "'") if(!$cs.Sys_IsMinimumRequiredDiskspaceAvailable('c:',100)){Exit-PSScript 3333} if ($global:DownloadPackage -and $InputObject){Start-PSDownloadPackage} $cs.Job_WriteLog("[Init]: `$PackageRoot:` '" + $Packageroot + "'") $cs.Job_WriteLog("[Init]: `$AppName:` '" + $AppName + "'") $cs.Job_WriteLog("[Init]: `$AppRelease:` '" + $AppRelease + "'") $cs.Job_WriteLog("[Init]: `$LogFile:` '" + $LogFile + "'") $cs.Job_WriteLog("[Init]: `$TempFolder:` '" + $TempFolder + "'") $cs.Job_WriteLog("[Init]: `$DllPath:` '" + $DllPath + "'") $cs.Job_WriteLog("[Init]: `$global:DownloadPackage`: '" + $global:DownloadPackage + "'") $domainInfo=Get-CimInstance -Namespace root\cimv2 -Class Win32_ComputerSystem if ($domainInfo.PartOfDomain -eq $false){ Import-Module Microsoft.PowerShell.Management -UseWindowsPowerShell -NoClobber -WarningAction:SilentlyContinue $cred=New-Object System.Management.Automation.PsCredential $global:username,($global:encpass | ConvertTo-SecureString -key $global:aeskey) if ($global:ou) { add-computer -domainname $global:domain -credential $cred -oupath $global:ou} else { add-computer -domainname $global:domain -credential $cred} } else{ $cs.Job_WriteLog("Computer is already joined to domain: $($domainInfo.Domain)") } Exit-PSScript $Error } catch { $line = $_.InvocationInfo.ScriptLineNumber $cs.Job_WriteLog("*****************","Something bad happend at line $($line): $($_.Exception.Message)") Exit-PSScript $_.Exception.HResult }
Enable local administrator account and set password.
[CmdletBinding()] Param( [Parameter(Mandatory=$true)] [string]$Packageroot, [Parameter(Mandatory=$true)] [string]$AppName, [Parameter(Mandatory=$true)] [string]$AppRelease, [Parameter(Mandatory=$true)] [string]$LogFile, [Parameter(Mandatory=$true)] [string]$TempFolder, [Parameter(Mandatory=$true)] [string]$DllPath, [Parameter(Mandatory=$false)] [Object]$InputObject=$null ) try { $global:aeskey=@(177,185,19,134,62,241,80,22,70,244,23,168,237,185,210,199,58,255,173,93,250,159,101,244,247,44,211,43,100,217,96,38) $global:encpass='25892d1116743f0423413b16050a5345MgB8AGYAWgAwAGoAdAB1AHoANQBzAHUANgAwAHMAOQBWAEgAYgBSAG4AbwBsAFEAPQA9AHwAMAA2AGUAOQBjADkAZAAyAGYANQBkAGUAMAA3ADEAMgA0ADQAMAAzAGYAYQBjAGYAOAA5AGUAOQBjAGEAMQBmAGUANwA3AGQANABmADQAYQAwADQAZgA4ADcAYQA5AGMAMAAxADUANQAwAGMAYQA5ADkAZABhAGIAZAAzAGUANAA=' ### Download package kit [bool]$global:DownloadPackage = $false ############################################## #load core PS lib - don't mess with this! if ($InputObject){$pgkit=""}else{$pgkit="kit"} Import-Module (Join-Path $Packageroot $pgkit "PSlib.psm1") -ErrorAction stop #load Library dll $cs=Add-PSDll ############################################## #Begin $cs.Job_Start("WS",$AppName,$AppRelease,$LogFile,"INSTALL") $cs.Job_WriteLog("[Init]: Starting package: '" + $AppName + "' Release: '" + $AppRelease + "'") if(!$cs.Sys_IsMinimumRequiredDiskspaceAvailable('c:',1500)){Exit-PSScript 3333} if ($global:DownloadPackage -and $InputObject){Start-PSDownloadPackage} $cs.Job_WriteLog("[Init]: `$PackageRoot:` '" + $Packageroot + "'") $cs.Job_WriteLog("[Init]: `$AppName:` '" + $AppName + "'") $cs.Job_WriteLog("[Init]: `$AppRelease:` '" + $AppRelease + "'") $cs.Job_WriteLog("[Init]: `$LogFile:` '" + $LogFile + "'") $cs.Job_WriteLog("[Init]: `$TempFolder:` '" + $TempFolder + "'") $cs.Job_WriteLog("[Init]: `$DllPath:` '" + $DllPath + "'") $cs.Job_WriteLog("[Init]: `$global:DownloadPackage`: '" + $global:DownloadPackage + "'") $cs.Job_WriteLog("Enable local Administrator") Enable-LocalUser -Name 'Administrator' -ErrorAction:Ignore Set-LocalUser -Name 'Administrator' -Password ($global:encpass | ConvertTo-SecureString -key $global:aeskey) -AccountNeverExpires $cs.Job_WriteLog("Successfully enabled local Administrator") Exit-PSScript $Error } catch { $line = $_.InvocationInfo.ScriptLineNumber $cs.Job_WriteLog("*****************","Something bad happend at line $($line): $($_.Exception.Message)") Exit-PSScript $_.Exception.HResult }