Also, for debugging.
If you put the script into a function and add [cmdletBinding()] you can run it with -debug
which means you can add lines like Write-Debug “variables is $variable”; that will only show up when -debug is specified and will pause for you. You can also add Write-Verbose “messages”; that only show
i.e. to do it while still running it as a ps1 script with arguments you’d have to be a little tricky. If you mad the script a .psm1 and added a export-modulemember -function funcName; line at the end and then ran this in a ps console ipmo -Force -Global \path\to\psm1; you could then run your script as a function that you can import into any script or console and add the -Debug and or -Debug lines directly when running the function
param (
[String]$programme, [switch]$debug, [switch]$verbose
)
function installProgramme {
[CmdletBinding()]
param (
[String] $programme
)
$user = "install"
$pwd = "1234500000000000000000000000000000000000000000000000000000AAAA="
$serveur = "\\fileserver.istic.univ-rennes1.fr\partage"
$cert = $(Get-ChildItem cert:\CurrentUser\TrustedPublisher | where {$_.Subject -eq "CN=ISTIC/ESIR Signature"})
$tab_key = @()
foreach ($i in $cert.SerialNumber.ToCharArray()){$tab_key += [convert]::ToInt16($i,16)}
$password = ConvertTo-SecureString -key $tab_key -string $pwd
$credential = New-Object -TypeName system.management.Automation.PSCredential -ArgumentList $user, $password
Write-Verbose "Attempting to mount $serveur...";
#net use p: $dossier_partage /p:n /u:$($credential.GetNetworkCredential().username) $($credential.GetNetworkCredential().password)
if (!(Test-Path -Path p:)){
$net = new-object -ComObject WScript.Network
$net.MapNetworkDrive("p:", $serveur, $false, $credential.GetNetworkCredential().UserName,$credential.GetNetworkCredential().password)
}
Write-Debug "Check if P is mounted...";
#lorsque l'on lance un script powershell, si il y avait des espaces dans le nom, cela ne passait pas
#lorsque l'on faisait un start-process et ce nom en argument. Donc on utilise plutot le nom court
$prog_court = (New-Object -ComObject Scripting.FileSystemObject).GetFile($programme).ShortPath
write-host "$(hostname):Dossier de l'installer $($dossier_installer)"
write-host ""
write-host "$(hostname):lancement de $($programme)"
write-host "$(hostname):lancement de $($prog_court)"
#start-process -FilePath $programme -wait -NoNewWindow
$dossier_installer = $((get-item -path $programme).DirectoryName)
if (!(Test-Path -Path "$dossier_installer\logs_fog_install")){New-Item -ItemType directory -Path "$dossier_installer\logs_fog_install"}
$extension = (get-item -path $programme).Extension
if ($extension -eq ".bat" -or $extension -eq ".cmd") {
#write-host "$env:COMPUTERNAME:C'est un script bat"
start-process -FilePath $prog_court -wait -NoNewWindow -RedirectStandardOutput ${dossier_installer}\logs_fog_install\${env:COMPUTERNAME}_log.txt -RedirectStandardError ${dossier_installer}\logs_fog_install\${env:COMPUTERNAME}_error.txt
}
if ($extension -eq ".ps1") {
#write-host "$env:COMPUTERNAME:C'est un script powershell"
$policy = Get-ExecutionPolicy
Set-ExecutionPolicy AllSigned
start-process -FilePath PowerShell -Arg $prog_court -wait -NoNewWindow -RedirectStandardOutput ${dossier_installer}\logs_fog_install\${env:COMPUTERNAME}_log.txt -RedirectStandardError ${dossier_installer}\logs_fog_install\${env:COMPUTERNAME}_error.txt
Set-ExecutionPolicy $policy
}
#net use p: /delete
$net.RemoveNetworkDrive("p:")
}
#create string to run function
$runFunc = "installProgramme $programme";
if($debug){
$runFunc += " -Debug";
}
if($verbose) {
$runFunc += " -Verbose";
}
$runFunc += ";";
Invoke-Expression $runFunc;