Powershell and VB script to update a registry key if it exists on the local system
Here are a couple scripts I wrote to update a registry key on a system if it the key exists. I created a PowerShell script to do this then realized that some of the systems being updated were pretty old so decided to go with VB Script Power Shell Script
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
$keypath = "HKLM:\SOFTWARE\SomeSoftware\Key" $keypath32 = "HKLM:\SOFTWARE\Wow6432Node\SomeSoftware\Key" $regkey = "key" $keyvalue = "keyValue" Push-Location if(Test-Path $keypath) { Set-Location $keypath Set-ItemProperty .$regkey $keyvalue if(Test-Path $keypath32) { Set-Location $keypath32 Set-ItemProperty .$regkey $keyvalue } } elseif(Test-Path $keypath32) { Set-Location $keypath32 Set-ItemProperty .$regkey $keyvalue } Pop-Location exit 0 |
VB Script
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
Const HKCR = &H80000000 'HKEY_CLASSES_ROOT Const HKCU = &H80000001 'HKEY_CURRENT_USER Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE Const HKUS = &H80000003 'HKEY_USERS Const HKCC = &H80000005 'HKEY_CURRENT_CONFIG RegkeyPath = "SOFTWARE\SomeSoftware\Key" RegkeyPath32 = "SOFTWARE\Wow6432Node\SomeSoftware\Key" RegKey = "Key" keyvalue = "KeyValue" Function KeyExists(Key, KeyPath) Dim oReg: Set oReg = GetObject("winmgmts:!root/default:StdRegProv") If oReg.EnumKey(Key, KeyPath, arrSubKeys) = 0 Then KeyExists = True Else KeyExists = False End If End Function Dim oReg: Set oReg = GetObject("winmgmts:!root/default:StdRegProv") if KeyExists(HKLM, RegkeyPath) = True Then if KeyExists(HKLM, RegkeyPath32) = True Then oReg.SetStringValue HKLM, RegKeyPath32, RegKey, keyvalue end if oReg.SetStringValue HKLM, RegKeyPath, RegKey, keyvalue elseif KeyExists(HKLM, RegKeyPath32) = True Then oReg.SetStringValue HKLM, RegKeyPath32, RegKey, keyvalue else end if |
[…]
Powershell Script to detect one or more KB articles are installed
This PowerShell script checks OS version or KB articles are installed on system. Used in SCCM as a detection method for IE 11 Prereqs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
$osresult = $null $os = (Get-WmiObject Win32_OperatingSystem).Name if ($os -like "*Windows 8*") {$osresult = "True"} elseif ($os -like "*Server 2012*") {$osresult = "True"} else{ $osresult = get-hotfix -id KB2670838,KB2729094,KB2786081,KB2834140 -ea 0 if ($osresult -like "*KB2670838*" -and $osresult -like "*KB2729094*" -and $osresult -like "*KB2786081*" -and $osresult -like "*KB2834140*"){ $osresult = "True" } else { $osresult = $null } } $osresult |
Powershell Install multiple Windows Updates .MSU
This script will loop through multiple msu windows updates files and install them.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
############################################## # # Name: PrereqInstaller.ps1 # Version: v0.1 # Date: 3/13/2014 # Purpose: Install multiple Microsoft KB#### *.msu # Author: Randy Gray # # # Folder Structure # # source # ├-- PrereqInstaller.ps1 # ├-- Win7 # | ├-- x64 # | | | KBxxxxxxa # | | | └ Windows6.1-KBxxxxxxx-x64.msu # | | └ KBxxxxxxb # | | └ Windows6.1-KBxxxxxxx-x64.msu # | └-- x86 # | └ Windows6.1-KBxxxxxxx-x86.msu # └-- Win81 # ├-- x64 # | └ KBxxxxxxx # | └ Windows8.1-KBxxxxxxx-x64.msu # └-- x86 # └ Windows8.1-KBxxxxxxx-x86.msu # # # Variables # $path - directy scritpt was run from. # $msus - array of windows updates # $msu - current windows udpate # $update - array of file name spilt by '-' # $kbart - current update KB name # $Hotfix - result returned during installed check # $command - command to install update # $parameters - command plus the parameters to the install command '\quiet \norestart' # $install - process to start installation # $OS - Current OS version # #folder - current folder of under path that contains the udpates. # ############################################### function Install-MSU($path) { # get updates in folders $msus = ls -Path $path *.msu -Recurse # loop through updates foreach ($msu in $msus) { # spilt file name to get KB artical number $update = $msu.Name -Split'-' $kbart = $update[1] # check if update is already installed $HotFix = Get-HotFix -id $kbart -ea 0 # run if update is not installed if($HotFix -eq $null) { Write-Host "Installing $kbart" $command = "`"" + "$path\$msu" + "`"" $parameters = $command + "\quiet \norestart" $install = [System.Diagnostics.Process]::Start( "wusa",$parameters ) $install.WaitForExit() } # run if update is installed else { Write-Host "Update $kbart installed" } } } # set $path to current directory location $path = get-location # set $OS to current OS $OS = gwmi -query "select Caption, OSArchitecture from win32_OperatingSystem" # if OS is windows 7 if($OS.Caption -match 'Windows 7') { if($OS.OSArchitecture -match '64-bit') { $folder = 'win7\x64' $path = "$path\$folder" Install-MSU($path) } else { $folder = "win7\x86" $path = "$path\$folder" Install-MSU($path) } } # if OS is windows 8.1 elseif($OS.Caption -match 'Windows 8.1') { if($OS.OSArchitecture -match '64-bit') { $folder = 'win81\x64' $path = "$path\$folder" Install-MSU($path) } else { $folder = "win81\x86" $path = "$path\$folder" Install-MSU($path) } } else { } |
PowerShell Set user permissions on a shared folder
Set user permissions on a shared folder using PowerShell Applies To: Windows 8.1, Windows PowerShell 4.0, Windows Server 2012 R2
1 |
Grant-SmbShareAccess -name ShareName -CimSession ComputerName -AccountName AccountName -AccessRight Full -Force |
C# Change service start mode
Here is some code to Change the StartMode on one or more services using C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
string ns = @"root\cimv2"; string query = "select * from Win32_Service"; string compname = "ComputerName"; ConnectionOptions options = new ConnectionOptions(); ManagementScope scope = new ManagementScope(string.Format(@"\\{0}\{1}", compname, ns), options); scope.Connect(); ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, new ObjectQuery(query)); ManagementObjectCollection retObjectCollection = searcher.Get(); foreach (ManagementObject mo in searcher.Get()) { name = mo.Properties["Name"].Value.ToString().Trim().ToLower(); state = mo.Properties["State"].Value.ToString().Trim(); startmode = mo.Properties["StartMode"].Value.ToString().Trim(); switch (name) { case "service1": changemode(mo, "Manual"); break; case "winmgmt": changemode(mo, "Auto"); break; } } private void changemode(ManagementObject mo, string startmode) { ManagementBaseObject inParams = mo.GetMethodParameters("ChangeStartMode"); inParams["startmode"] = startmode; ManagementBaseObject outParams = mo.InvokeMethod("ChangeStartMode", inParams, null); startmode = mo.Properties["StartMode"].Value.ToString().Trim(); } |
C# Start service on remote machine
Here is a simple method to start a service on a remote system using C#. Call the method using
1 |
string status = servicestart("computerName", "Service"); |
Method will attempt to change the status and return the current Status
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
private string servicestart(string compname, string service) { ServiceController sc = new ServiceController(); sc.ServiceName = service; sc.MachineName = compname; string result = sc.Status.ToString(); if (sc.Status == ServiceControllerStatus.Stopped) { try { sc.Start(); sc.WaitForStatus(ServiceControllerStatus.Running); result = sc.Status.ToString(); } catch (InvalidOperationException) { // do something } } return result; } |
CCMSETUP FAILED WITH ERROR CODE 0x8007064c
Upgrading from Client version 4.00 to 5.00 I got an error “CcmSetup failed with error code 0x8007064c”
1 2 |
Installation failed with error code 1612 ccmsetup 5/14/2013 2:18:11 PM 3064 (0x0BF8) InstallFromManifest failed 0x8007064c ccmsetup 5/14/2013 2:18:11 PM 3064 (0x0BF8) |
after looking at the logs I noticed there were two instances of 4.00 being detected.
1 2 |
Upgrade code '{252DA259-82CA-4177-B8D0-49C78937BA3E}': product = '{F8B99F09-BC56-4460-9B40-425C0DF82FDB}', installed = 1, version = 4.00.6425.2000 ccmsetup 5/14/2013 2:18:11 PM 3064 (0x0BF8) Upgrade code '{252DA259-82CA-4177-B8D0-49C78937BA3E}': product = '{2609EDF1-34C4-4B03-B634-55F3B3BC4931}', installed = 1, version = 4.00.6487.2000 ccmsetup 5/14/2013 2:18:11 PM 3064 (0x0BF8) |
Both instances are showing up in the registry under HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products\ but when I checked the source path it no longer existed. […]
CcmSetup failed with error code 0X80070641
Today when I was trying to install the SCCM Client on a system and was getting an error code of 0x80070641. It was failing trying to install vcredist_x86.exe with an error code of 1601 – here is the log entry for the error.
1 2 |
File 'C:\Windows\ccmsetup\vcredist_x86.exe' returned failure exit code 1601. Fail the installation. ccmsetup 5/9/2013 5:41:23 PM 2688 (0x0A80) InstallFromManifest failed 0x80070641 ccmsetup 5/9/2013 5:41:23 PM 2688 (0x0A80) |
In my case the Windows Installer service was not running. so I […]
PowerShell WMI query to get IP address
Here is a simple function in PowerShell to ping a system via WMI query and return the IP address.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
function Ping-Host { Param([string]$computername=$(Throw "No system name provided.")) $query="Select * from Win32_PingStatus where address='$computername'" $wmi=Get-WmiObject -query $query write $wmi } $computer = "COMPUTER1" #ping the computer if ($pingResult) { #clear PingResult if it has a left over value Clear-Variable pingResult } $pingResult=Ping-Host $computer Write-Debug "Pinged status code is $($pingResult.Statuscode)" if ($pingResult.StatusCode -eq 0) { Write-Host "Query successful on " $computer " - IP: " $pingResult.ProtocolAddress #get remaining information via WMI Trap { #define a trap to handle any WMI errors Write-Warning ("Problem with {0}" -f $computer.toUpper()) Write-Warning $_.Exception.GetType().FullName Write-Warning $_.Exception.message Continue } } |