This is an example on how to create and call a Powerpack script from a CapaInstaller script (.cis)
insert code in a .cis script to call a powershell install scriptAll PowerPacks scripts must end with Exit-PSScript $Error or they will fail.
Code Block | ||
---|---|---|
| ||
Exit-PSScript $Error
|
How to suppress recording any errors when using PowerShell commands (if supported).
Code Block | ||
---|---|---|
| ||
Get-Process -Name 'dummy' -ErrorAction:Ignore
|
Create Functions - it is recommended to use the “approved” naming convention when creating functions. (Verb-Noun)
Code Block | ||
---|---|---|
| ||
If bStatus Then bStatus = File_FindFile("pwsh.exe", gsProgramfiles & "\PowerShell", True)
If bStatus and isArray(gaValue) Then sPwshPath=gaValue(0)
If bStatus and sPwshPath<>"" Then
bStatus = File_GetVersion(sPwshPath, True)
sPwshVersion=gsValue
call Job_WriteLog("***********","Powershell installed: " & sPwshPath,true,true)
call Job_WriteLog("***********","Powershell version installed: " & sPwshVersion,true,true)
Else
bStatus=Job_ErrorHandling("***********","Powershell (Core) must be installed.", 0)
End if
PSArgs="-Packageroot " & """" & gsPackageroot & """"
PSArgs=PSArgs & " -AppName " & """" & sAppName & """"
PSArgs=PSArgs & " -DllPath " & """" & gsWorkstationPath & "Lib\CapaOne.ScriptingLibrary.dll" & """"
PSArgs=PSArgs & " -AppRelease " & """" & sPackageVersion & """"
PSArgs=PSArgs & " -LogFile " & """" & gsLogdir & sAppName & "\" & sPackageVersion & "\" & sAppName & ".ps_Install.log" & """"
PSArgs=PSArgs & " -TempFolder " & """" & gsTempdir & """"
If bStatus Then bStatus=shell_execute(sPwshPath,"-ExecutionPolicy bypass -file " & """" & gsPackageroot & "\kit\Install.ps1" & """" & " " & PSArgs,True,0,False)
If bStatus And giErrorlevel=3326 Then bStatus=CMS_JobRetryLater("Package will retry later.")
If bStatus And giErrorlevel=3330 Then giErrorlevel=0
If bStatus And giErrorlevel=3010 Then giErrorlevel=0
If bStatus Then bStatus=cbool(giErrorlevel=0) |
powershell script example (pslib.psm1 is a powerpack ps module and is used to load scripting library dll and handles exit codes)
View file | ||
---|---|---|
|
Code Block | ||
---|---|---|
| ||
| ||
# 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.
Code Block | ||
---|---|---|
| ||
# Correct
Start-InitializeSDK
# Wrong
Start-InitializeSDK() |
How to encrypt passwords.
Code Block | ||
---|---|---|
| ||
[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.
Code Block | ||
---|---|---|
| ||
[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.
Code Block | ||
---|---|---|
| ||
[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 variables [string]$global:ProductVersion = "1.65.2" [string]$global:LogFolder = Split-Path $LogFile -Parent [string]$global:InstallFile = "$Packageroot\kit\VSCodeSetup-x64-$global:ProductVersion.exe" [string]$global:InstallArgs = "/VERYSILENT /NORESTART /LANG=en /MERGETASKS=!runcode,addcontextmenufiles,addtopath /LOG=`"$(Join-Path $global:LogFolder $AppName).app.log`"" ############################################## 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 Stopstop #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:',3001500)){Exit-PSScript 3333} if ($global:DownloadPackage -and $InputObject){Start-PSDownloadPackage} $cs.Job_WriteLog("[Init]: `$PackageRoot:` '" $retvalue=+ $Packageroot + "'") $cs.ShellJob_Execute($global:InstallFile,$global:InstallArgs,$true,0,$trueWriteLog("[Init]: `$AppName:` '" + $AppName + "'") $cs.Job_WriteLog("[Init]: if ($retvalue -ne 0){Exit-PSScript $retvalue} `$AppRelease:` '" + $AppRelease + "'") $cs.Job_WriteLog("[Init]: `$LogFile:` '" + $LogFile + "'") $cs.Job_WriteLog("[Init]: `$TempFolder:` '" + $TempFolder + "'") $cs.Job_WriteLog("Install:","$AppName completed with status: $retvalue") [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 } |