Adventures in Powershell. These posts have been created as a record of the successful powershell commands I have used.
Thursday, November 8, 2018
$LASTEXITCODE
$LASTEXITCODE is a preexisting variable which takes on the exit / error code of a command.
In this snippet powershell calls the vbscript items and adds the exit codes together to produce a final exit code total
$LASTEXITCODE = 0
$ErrorCode = $null
$Install1 = "cscript .\inst1.vbs"
$Install2 = "cscript .\inst2.vbs"
Invoke-Expression $Install1
$ErrorCode += $LASTEXITCODE
Invoke-Expression $Install2
$ErrorCode += $LASTEXITCODE
$ErrorCode
exit $ErrorCode
Wednesday, October 31, 2018
Compress and Expand Zips
Powershell 5.0 has cmdlets to handle file compression.
# Create a zip file with the contents of C:\Stuff\
Compress-Archive -Path C:\Stuff -DestinationPath archive.zip
# Add more files to the zip file
# (Existing files in the zip file with the same name are replaced)
Compress-Archive -Path C:\OtherStuff\*.txt -Update -DestinationPath archive.zip
# Extract the zip file to C:\Destination\
Expand-Archive -Path archive.zip -DestinationPath C:\Destination
above is from
https://stackoverflow.com/questions/1153126/how-to-create-a-zip-archive-with-powershell
Friday, September 21, 2018
I gotta split
#Locate the last split value in this case a folder from a path of unknown length.
$UserProfile = $env:USERPROFILE
#This item splits up the user profile into any given number items using the \ character as the charcter to split at.
$UserProfileSplit = $UserProfile.Split("\")
#The counts the number of split items generated in to the $UserProfileSplit array
$UserProfileSplit | measure | select -expand count
$UserProfileFolder = $UserProfileSplit | select -last 1
$UserProfile = $env:USERPROFILE
#This item splits up the user profile into any given number items using the \ character as the charcter to split at.
$UserProfileSplit = $UserProfile.Split("\")
#The counts the number of split items generated in to the $UserProfileSplit array
$UserProfileSplit | measure | select -expand count
$UserProfileFolder = $UserProfileSplit | select -last 1
Wednesday, June 27, 2018
Check if a process is running
#See if Excel is running.
#Do not exit until it is not running.
$CheckExcelRunning=Get-Process | select -ExpandProperty processname
While ($CheckExcelRunning -contains "EXCEL")
{
$CheckExcelRunning=Get-Process | select -ExpandProperty processname
if ($CheckExcelRunning -contains "EXCEL")
{
#Wait 5 minutes before next check
start-sleep -S 300
}
}
exit
Thursday, January 4, 2018
Call a command line command and process the output
Powershell scripts can call standard command line commands and then process the output.
$FSUoutput = fsutil.exe 8dot3name query c:
$DIRXoutput = cmd.exe /c dir C:\ /x
foreach ($line in $DIRXoutput)
{
if ($line -like "*progra~1*") {$8DOT3NamesExist=1}
}
if ($8DOT3NamesExist -ne 1) {$ERRORCODE+=1}
foreach ($line in $FSUoutput)
{
#Checks FSUtil output reports C: is 8dot3 enabled
if ($line.StartsWith("The volume state is"))
{
#Checking for 0
$line=$line.Remove(22)
$line=$line.TrimStart("The volume state is: ")
$line = $line/1
$CVSTATE = $line
if ($CVSTATE -ne 0) {$ERRORCODE+=$CVSTATE*10}
}
#Checks fSUtil output reports what the system registry setting is
if ($line -isnot [int] -and $line.StartsWith("The registry state is: "))
{
$line=$line.Remove(24)
$line=$line.TrimStart("The registry state is: ")
$line = $line/1
$RSTATE = $line
if ($RSTATE -ne 2)
{
if ($RSTATE -eq 0) {$ERRORCODE+=100}
if ($RSTATE -eq 1) {$ERRORCODE+=200}
if ($RSTATE -eq 3) {$ERRORCODE+=300}
}
}
}
exit $ERRORCODE
Subscribe to:
Posts (Atom)