# Build 2 $root = $PSCommandPath | Split-Path -Parent . $root\config.ps1 $computerName = $env:COMPUTERNAME $UUID=(get-wmiobject Win32_ComputerSystemProduct).UUID $exists = Invoke-RestMethod -Method Get -Uri "$server/get/computerexists?ComputerName=$computerName&UUID=$UUID" if ($exists.Result -eq 0){ Write-Host "Computer outside database:" $computerName exit } $Timestamp = [int64](([datetime]::UtcNow)-(get-date "1/1/1970")).TotalSeconds # Hardware Data $SOData = Get-CimInstance Win32_OperatingSystem $SOVersion = $SOData.Version if ($SOVersion -match "^10.0."){ #If its Windows 10, add revision number for knowing hotfix installed $revi = -join(".",(Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion' UBR).UBR) $SOVersion += $revi } # Update Computer Data $CPUInfo = Get-WmiObject Win32_Processor $RAMInstalled = Get-WmiObject CIM_PhysicalMemory | Measure-Object -Property capacity -Sum | ForEach-Object {[math]::round(($_.sum / 1MB),2)} $paramInvoke = -join("$server/upd/computer?ComputerName=$computerName&UUID=",$UUID,"&SOVersion=",$SOVersion,"&SOCaption=",$SOData.Caption,"&SOBit=",$SOData.OsArchitecture.Substring(0,2),"&LastConnection=",$Timestamp,"&RAM=",$RAMInstalled,"&CPUName=",$CPUInfo.Name) Invoke-RestMethod -Method Get -Uri $paramInvoke | out-null # More info $lastUser = (Get-CimInstance -ClassName Win32_ComputerSystem -Property UserName -ComputerName .).UserName Invoke-RestMethod -Method Get -Uri "$server/upd/computer?ComputerName=$computerName&UUID=$UUID&LastUser=$lastUser" | out-null $cooks = Invoke-RestMethod -Method Get -Uri "$server/get/cook?ComputerName=$computerName&UUID=$UUID" foreach ($CookName in $cooks){ Set-Location $env:temp # For downloading/copying items, use system temp location $CookName = $CookName.CookName $cook = Invoke-RestMethod -Method Get -Uri "$server/load/cook?CookName=$CookName&ComputerName=$computerName&UUID=$UUID" Write-Host "Receta:" $cook.name $CookRevision = $cook.revision $err = 0 $exit = 0 $inif = $false $if = $true $filesCopied = New-Object System.Collections.ArrayList # For REPOTOLOCAL, has a list of files copied to delete when finish (Do not store forever in temp) foreach ($step in $cook.steps){ if ($err -eq 1 -and $noerror -eq 1){$err = 0; $errvar = ""} #If "noerror" is active, do not count errors if ($err -eq 1 -or $exit -eq 1){break} # Halt if err ocurred (And noerror directive is not active) $step = $step.Split("|") $param = $step[1] Write-Host $step[0] "-" $step[1] if($inif -eq $true -and $if -eq $false){ # Only can see "ENDIF" if is in IF and is not true if ($step[0] -ne "ENDIF" -and $step[0] -ne "ELSE"){ Write-Host $step[0] "Not executed, IF not meet" $step[0] = "" #Disable command $step[1] = "" } } switch ($step[0].ToUpper()) { #Command "UNINSTALL" { # Remove program Get-Package -Name "$param*" -ErrorAction Continue #This will return error if program is not installed, do not see it. if ($? -eq $true){ #If its True, is that package exists Get-Package -Name "$param*" -ErrorVariable errvar | Uninstall-Package -ErrorVariable errvar if ($? -eq $false){ # If fail then put 1 (When fail, powershell returns False) $err = 1 } } } "SERV_DISABLE" { # Disable a service Set-Service $param -StartupType Disabled -Status Stopped -ErrorVariable errvar if ($? -eq $false){ #If its False, it was a problem $err = 1 } } "SERV_ENABLE" { # Enable a service Set-Service $param -StartupType Automatic -Status Running -ErrorVariable errvar if ($? -eq $false){ #If its False, it was a problem $err = 1 } } "KILL_PROCESS" { $p = Get-Process -Name "$param" if ($? -eq $true){ # Only do something if exists Stop-Process -InputObject $p -Force -ErrorVariable errvar -ErrorAction Continue #if ($p.HasExited -eq $false){ # $err = 1 #} } } "CMD" { # Run a cmd command. Note: Runs at high priv. cmd.exe /c "$param" if ($? -eq $false){ # Error in CMD $err = 1 } } "PWCMD" { # Run a powershell command. Note: Runs as high priv. Invoke-Expression $param if ($? -eq $false){ # Error in CMD $err = 1 } } "REPOTOLOCAL" { # Copy file from repo location to local ($env:temp) for use in cmd or other things Copy-Item "$resources\$param" $env:temp -ErrorVariable errvar -Recurse -Force if ($? -eq $false){ # Error in Copy $err = 1 } $filesCopied.Add($param) > $null #Add to list } "REMOVE" { # Remove files / folders Remove-Item "$param" -Recurse -Force -ErrorAction Continue # They not see errors (Because error will be file not found) } "INSTALLMSI" { # Installs a .msi file (From $env:temp) Start-Process msiexec.exe -Wait -ArgumentList "/norestart /quiet /I $param" -ErrorVariable errvar if ($? -eq $false){ # Error in MSI $err = 1 } } "REGFILE" { # Imports a .reg file reg import .\$param if ($? -eq $false){ # Error importing reg file $err = 1 } } "MSG" { # Display a message msg * "$param" } {$_ -in "SLEEP","PAUSE"}{ # Pause exec some seconds [int]$secs = $param Start-Sleep -Seconds $secs } "NOERROR" { #All within NOERROR doesn't generate errors and stop scripts $noerror = 1 } "ENDNOERROR" { $noerror = 0 } "IFSOFTWAREINST" { # If with software $inif = $true #This controls IF start/stop Get-Package -Name "$param*" -ErrorAction SilentlyContinue #This will return error if program is not installed, do not see it. $if=$? # True -> Exists ; False -> Not exists } "IFSOFTWAREVER" { # If with software $inif = $true #This controls IF start/stop $parts = $param.Split(";") if ($parts[1] -ne ""){ #Exists uri and filename $p_name = $parts[0] $p_ver = $parts[1] Get-Package -Name "$p_name*" -MinimumVersion "$p_ver" -MaximumVersion "$p_ver" -ErrorAction SilentlyContinue #This will return error if program is not installed, do not see it. $if=$? # True -> Exists ; False -> Not exists }else{ #Doesn't sent right $err = 1 $errvar = "Param not set right. Exiting..." } } "IFPATHEXISTS" { # If only if a path exists (File, Folder, Registry Key..) $inif = $true #This controls IF start/stop Test-Path $param -PathType Any -ErrorAction SilentlyContinue $if=$? } "IFPWCMD" { # If with powershell command $inif = $true #This controls IF start/stop Invoke-Expression $param #Executes powershell command $if=$? # True -> Exists ; False -> Not exists } "ELSE" { # Turn bool $if $if = !$if } "ENDIF"{ # End the if $inif = $false $if = $true } "DOWNLOAD" { #Download a file. This is a bit problematic one, will use ; to separate download and filename, and its forced to use it.. $parts = $param.Split(";") if ($parts[1] -ne ""){ #Exists uri and filename $progressPreference = 'silentlyContinue' Invoke-WebRequest -Uri $parts[0] -OutFile $parts[1] -UseBasicParsing -ErrorVariable errvar $progressPreference = 'Continue' $filesCopied.Add($parts[1]) > $null #Add to list }else{ #Doesn't sent right $err = 1 $errvar = "Param not set right. Exiting..." } } "EXIT"{ # Exits cook completly. If some param, exit will be with "error" if ($param){ #Exit with error message $noerror = 0 $err = 1 $errvar = $param $exit = 1 }else{ #Exit as sucessful $err = 0 $errvar = "" $exit = 1 } } Default {} } } # Send results if ($errvar){ #There is an error if this has something $errvar = $($errvar | Out-String) $errvar = [System.Convert]::ToBase64String([System.Text.Encoding]::UNICODE.GetBytes($errvar)) $err = 1 }else{ $errvar="" } if ($cook.runever -eq "1"){ $err = -1 # This is for run but not for error, is by cook saw. } Invoke-RestMethod -Method Get -Uri "$server/set/cookstatus?ComputerName=$computerName&UUID=$UUID&CookName=$CookName&Revision=$CookRevision&Error=$err&ErrorDesc=$errvar" | out-null #Delete files copied to temp for saving space foreach ($element in $filesCopied) { Remove-Item "$env:temp\$element" -ErrorAction SilentlyContinue -Recurse -Force } }