Adventures in Powershell. These posts have been created as a record of the successful powershell commands I have used.
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
Monday, December 4, 2017
Read Registry keys using WMI
This code can retrieve the registry values of specified keys using WMI
$objReg = [WMIClass] "\\$TargetName\root\default:StdRegProv"
$HKEY_LOCAL_MACHINE = 2147483650
""
"WMI/Registry Reported App-V Client Version"
$AppVClientVersion = $null
$TargetKey = "SOFTWARE\Microsoft\AppV\Client"
$TargetKeyData="Version"
$TargetValue = $objReg.GetStringValue($HKEY_LOCAL_MACHINE, $targetkey, $targetkeydata)
$AppVClientVersion = $TargetValue.sValue
if ($AppVClientVersion -notlike $null)
{
$AppVClientVersion
}
else
{"AppV Client Version Value Not Found"
}
The number codes for the other registry locations are:
HKEY_CLASSES_ROOT (2147483648)
HKEY_CURRENT_USER (2147483649)
HKEY_LOCAL_MACHINE (2147483650)
HKEY_USERS (2147483651)
HKEY_CURRENT_CONFIG (2147483653)
Tuesday, April 11, 2017
Current Folder
$scriptDir = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
Add-AppvClientConnectionGroup $scriptDir"\CGDEF_WINPROJ.xml"
Enable-AppvClientConnectionGroup "Winproj Add-ins" -global
Thursday, December 8, 2016
Subscribe to:
Posts (Atom)