Running a .bat file on several servers - powershell

I am currently trying to run a .bat file on around 150 servers. I can get the script to run through as if there's no issues - the .bat copies to the servers, however it does not seem to be executing at all.
Running on windows 2012 servers mainly.
#Variables
$servers = "D:\Apps\Davetest\servers.txt"
$computername = Get-Content $servers
$sourcefile = "D:\Apps\Davetest\test.bat"
#This section will install the software
foreach ($computer in $computername)
{
$destinationFolder = "\\$computer\C$\Temp"
<#
It will copy $sourcefile to the $destinationfolder. If the Folder does
not exist it will create it.
#>
if (!(Test-Path -path $destinationFolder))
{
New-Item $destinationFolder -Type Directory
}
Copy-Item -Path $sourcefile -Destination $destinationFolder
Invoke-Command -ComputerName $computer -ScriptBlock {Start-Process 'c:\Temp\test.bat'}
}
I am looking for it to run the .bat once it hits the servers and currently it only seems to be copying over.

That's because Start-Process immediately returns. Use the -Wait Parameter.
Start-Process -FilePath 'c:\Temp\test.bat' -NoNewWindow -Wait -PassThru
microsoft:
-PassThru
Returns a process object for each process that the cmdlet started. By default, this cmdlet does not generate any output.
-Wait
Indicates that this cmdlet waits for the specified process and its descendants to complete before accepting more input. This parameter suppresses the command prompt or retains the window until the processes finish.
-PassThru returns you a process object, where you can check the ExitCode parameter:
$p = Start-Process -FilePath your_command -ArgumentList "arg1", "arg" -NoNewWindow -Wait -PassThru
if ($p.ExitCode -ne 0) {
throw "Failed to clone $buildItemName from $buildItemUrl to $($tmpDirectory.FullName)"
}
As an alternative to Start-Process you could also use Invoke-Expression which will return you the stdout of the console.
To check if Invoke-Expression was successful you can use:
$output = Invoke-Expression $command
if ((-not $?) -or ($LASTEXITCODE -ne 0)) {
throw "invoke-expression failed for command $command. Command output: $output"
}

Related

Install msi using Start-Process with /q switch?

I want to install an msi with a /q switch, I look online and the examples don't have the /q switch and I keep getting errors.
I need something like:
$WorkingDirectory = (Split-Path $myinvocation.mycommand.path -Parent)
Start-Process -FilePath msiexec /i "$WorkingDirectory\LAPS.x64.msi" -ArgumentList /q
Don't bother with Start-Process. Use the call operator:
& msiexec.exe /i "$WorkingDirectory\LAPS.x64.msi" /q
put the whole command in the brackets
Example(Python msi):
Start-Process msiexec.exe -Wait -ArgumentList "/I $($LocalPython.FullName) /passive ALLUSERS=1 ADDLOCAL=Extensions"
replace /passive with /q
Not all installers are the same. To find the installer switches for your .msi use:
.\LAPS.x64.msi /?
.\LAPS.x64.msi -?
I would also store the msi path in a variable, and use an ArrayList for the arguments, something like this has worked for me in my scripts:
# Path to .msi
$msiPath= 'C:\LAPS.x64.msi'
# Define arguments
[System.Collections.ArrayList]$arguments =
#("/i `"$msiPath`"",
"/quiet")
# Start installation
Start-Process -FilePath msiexec.exe -ArgumentList "$arguments" -Wait -NoNewWindow
$path="C:\Dat\install.msi"
$parameters="/q"
$packageinstall=(split-path $path -leaf) + ' ' + $parameters
write-host $packageinstall
$computers = get-content c:\com.txt
$computers | where{test-connection $_ -quiet -count 1} | ForEach-Object {
copy-item $path "\\$_\c$\windows\temp" -Force -Recurse
$newProc=([WMICLASS]"\\$_\root\cimv2:win32_Process").Create("C:\windows\temp\$packageinstall")
If ($newProc.ReturnValue -eq 0) {
Write-Host $_ $newProc.ProcessId
} else {
write-host $_ Process create failed with $newProc.ReturnValue
}
}

PowerShell SQL Job Step Move-Item not working on 1 server

This identical code has been used in 3 servers, and only one of them does it silently fail to move the items (it still REMOVES them, but they do not appear in the share).
Azure-MapShare.ps1
param (
[string]$DriveLetter,
[string]$StorageLocation,
[string]$StorageKey,
[string]$StorageUser
)
if (!(Test-Path "${DriveLetter}:"))
{
cmd.exe /c "net use ${DriveLetter}: ${StorageLocation} /u:${StorageUser} ""${StorageKey}"""
}
Get-Exclusion-Days.ps1
param (
[datetime]$startDate,
[int]$daysBack
)
$date = $startDate
$endDate = (Get-Date).AddDays(-$daysBack)
$allDays =
do {
"*"+$date.ToString("yyyyMMdd")+"*"
$date = $date.AddDays(-1)
} until ($date -lt $endDate)
return $allDays
Migrate-Files.ps1
param(
[string]$Source,
[string]$Filter,
[string]$Destination,
[switch]$Remove=$False
)
#Test if source path exist
if((Test-Path -Path $Source.trim()) -ne $True) {
throw 'Source did not exist'
}
#Test if destination path exist
if ((Test-Path -Path $Destination.trim()) -ne $True) {
throw 'Destination did not exist'
}
#Test if no files in source
if((Get-ChildItem -Path $Source).Length -eq 0) {
throw 'No files at source'
}
if($Remove)
{
#Move-Item removes the source files
Move-Item -Path $Source -Filter $Filter -Destination $Destination -Force
} else {
#Copy-Item keeps a local copy
Copy-Item -Path $Source -Filter $Filter -Destination $Destination -Force
}
return $True
The job step is type "PowerShell" on all 3 servers and contains this identical code:
#Create mapping if missing
D:\Scripts\Azure-MapShare.ps1 -DriveLetter 'M' -StorageKey "[AzureStorageKey]" -StorageLocation "[AzureStorageAccountLocation]\backup" -StorageUser "[AzureStorageUser]"
#Copy files to Archive
D:\Scripts\Migrate-Files.ps1 -Source "D:\Databases\Backup\*.bak" -Destination "D:\Databases\BackupArchive"
#Get date range to exclude
$exclusion = D:\Scripts\Get-Exclusion-Days.ps1 -startDate Get-Date -DaysBack 7
#Remove items that are not included in exclusion range
Remove-Item -Path "D:\Databases\BackupArchive\*.bak" -exclude $exclusion
#Move files to storage account. They will be destroyed
D:\Scripts\Migrate-Files.ps1 -Source "D:\Databases\Backup\*.bak" -Destination "M:\" -Remove
#Remove remote backups that are not from todays backup
Remove-Item -Path "M:\*.bak" -exclude $exclusion
If I run the job step using SQL then the files get removed but do not appear in the storage account. If I run this code block manually, they get moved.
When I start up PowerShell on the server, I get an error message: "Attempting to perform the InitializeDefaultDrives operation on the 'FileSystem' provider failed." However, this does not really impact the rest of the operations (copying the backup files to BackupArchive folder, for instance).
I should mention that copy-item also fails to copy across to the share, but succeeds in copying to the /BackupArchive folder
Note sure if this will help you but you could try to use the New-PSDrive cmdlet instead of net use to map your shares:
param (
[string]$DriveLetter,
[string]$StorageLocation,
[string]$StorageKey,
[string]$StorageUser
)
if (!(Test-Path $DriveLetter))
{
$securedKey = $StorageKey | ConvertTo-SecureString -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential ($StorageUser, $securedKey)
New-PSDrive -Name $DriveLetter -PSProvider FileSystem -Root $StorageLocation -Credential $credentials -Persist
}
Apparently I tricked myself on this one. During testing I must have run the net use command in an elevated command prompt. This apparently hid the mapped drive from non-elevated OS features such as the Windows Explorer and attempts to view its existence via non-elevated command prompt sessions. I suppose it also was automatically reconnecting during reboots because that did not fix it.
The solution was as easy as running the net use m: /delete command from an elevated command prompt.

Install .msi remotely using Powershell

I have made he following code using the code present on this forum.
cls
$computername = Get-Content 'C:\Users\C201578-db\Documents\server.txt'
$sourcefile = "\\iceopsnas\LNT_SoftwareRep.grp\CORE\COTS\EMC\Avamar\Avamar_7.0\CR06794393\AvamarClient-windows-x86_64-7.0.102-47.msi"
#This section will install the software
foreach ($computer in $computername)
{
$destinationFolder = "\\$computer\C$\Avamar"
#This section will copy the $sourcefile to the $destinationfolder. If the Folder does not exist it will create it.
if (!(Test-Path -path $destinationFolder))
{
New-Item $destinationFolder -Type Directory
}
Copy-Item -Path $sourcefile -Destination $destinationFolder
Write-Host "Copied Successfully"
Invoke-Command -ComputerName $computer -ScriptBlock { & cmd /c "msiexec.exe /i C:\Avamar\AvamarClient-windows-x86_64-7.0.102-47.msi" /qb ADVANCED_OPTIONS=1 CHANNEL=100}
Write-Host "Installed Successfully"
}
I tried all permutations and combinations but no luck. Tried all the suggestions that I got while posting this question but nothing. The copy procedure is successful but the .msi file is not getting installed. Maybe this question gets marked duplicate but still suggest some edits before doing that.
try defining your command as a script block instead:
$command = "msiexec.exe /i C:\Avamar\AvamarClient-windows-x86_64-7.0.102-47.msi"
$scriptblock = [Scriptblock]::Create($command)
Invoke-Command -ComputerName $computer -ScriptBlock $scriptblock
As a workaround (the lack of details doesnt help to daignose the problem), you could use the third party tool psexec.exe to run the installer on the remote host.
Try to replace your invoke-command with
psexec.exe \\$computer -s -u Adminuser -p AdminPassword msiexec /i C:\Avamar\AvamarClient-windows-x86_64-7.0.102-47.msi /qb ADVANCED_OPTIONS=1 CHANNEL=100
It's working fine with psexec.exe, I have installed it on more than 100 user's desktop. Setup your user's ip addresses on clients.txt file. Below is my code :
cls
$computername = Get-Content 'C:\Setup\clients.txt'
$sourcefile = "C:\Setup\MySyncSvcSetup.msi"
$serviceName = "MySyncWinSvc"
$adminUserName = "username"
$adminPassword = "password#123"
#This section will install the software
foreach ($computer in $computername)
{
#First uninstall the existing service, if any
C:\PSTools\psexec.exe \\$computer -s -u $adminUserName -p $adminPassword msiexec.exe /x C:\SetupFiles\MySyncSvcSetup.msi /qb
Write-Host "Uninstalling Service"
$destinationFolder = "\\$computer\C$\SetupFiles"
#This section will copy the $sourcefile to the $destinationfolder. If the Folder does not exist it will create it.
if (!(Test-Path -path $destinationFolder))
{
New-Item $destinationFolder -Type Directory
}
Copy-Item -Path $sourcefile -Destination $destinationFolder
Write-Host "Files Copied Successfully"
C:\PSTools\psexec.exe \\$computer -s -u $adminUserName -p $adminPassword msiexec.exe /i C:\SetupFiles\MySyncSvcSetup.msi /qb /l* out.txt
Write-Host "Installed Successfully"
C:\PSTools\psexec.exe \\$computer -s -u $adminUserName -p $adminPassword sc.exe start $serviceName
Write-Host "Starting the Service"
}

PowerShell run script simultaneously

I created a PowerShell script to remove all files and folders older than X days. This works perfectly fine and the logging is also ok. Because PowerShell is a bit slow, it can take some time to delete these files and folders when big quantities are to be treated.
My questions: How can I have this script ran on multiple directories ($Target) at the same time?
Ideally, we would like to have this in a scheduled task on Win 2008 R2 server and have an input file (txt, csv) to paste some new target locations in.
Thank you for your help/advise.
The script
#================= VARIABLES ==================================================
$Target = \\share\dir1"
$OlderThanDays = "10"
$Logfile = "$Target\Auto_Clean.log"
#================= BODY =======================================================
# Set start time
$StartTime = (Get-Date).ToShortDateString()+", "+(Get-Date).ToLongTimeString()
Write-Output "`nDeleting folders that are older than $OlderThanDays days:`n" | Tee-Object $LogFile -Append
Get-ChildItem -Directory -Path $Target |
Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-$OlderThanDays) } | ForEach {
$Folder = $_.FullName
Remove-Item $Folder -Recurse -Force -ErrorAction SilentlyContinue
$Timestamp = (Get-Date).ToShortDateString()+" | "+(Get-Date).ToLongTimeString()
# If folder can't be removed
if (Test-Path $Folder)
{ "$Timestamp | FAILLED: $Folder (IN USE)" }
else
{ "$Timestamp | REMOVED: $Folder" }
} | Tee-Object $LogFile -Append # Output folder names to console & logfile at the same time
# Set end time & calculate runtime
$EndTime = (Get-Date).ToShortDateString()+", "+(Get-Date).ToLongTimeString()
$TimeTaken = New-TimeSpan -Start $StartTime -End $EndTime
# Write footer to log
Write-Output ($Footer = #"
Start Time : $StartTime
End Time : $EndTime
Total runtime : $TimeTaken
$("-"*79)
"#)
# Create logfile
Out-File -FilePath $LogFile -Append -InputObject $Footer
# Clean up variables at end of script
$Target=$StartTime=$EndTime=$OlderThanDays = $null
One way to achieve this would be to write an "outer" script that passes the directory-to-be-cleaned, into the "inner" script, as a parameter.
For your "outer" script, have something like this:
$DirectoryList = Get-Content -Path $PSScriptRoot\DirList;
foreach ($Directory in $DirectoryList) {
Start-Process -FilePath powershell.exe -ArgumentList ('"{0}\InnerScript.ps1" -Path "{1}"' -f $PSScriptRoot, $Directory);
}
Note: Using Start-Process kicks off a new process that is, by default, asynchronous. If you use the -Wait parameter, then the process will run synchronously. Since you want things to run more quickly and asynchronously, omitting the -Wait parameter should achieve the desired results.
Invoke-Command
Alternatively, you could use Invoke-Command to kick off a PowerShell script, using the parameters: -File, -ArgumentList, -ThrottleLimit, and -AsJob. The Invoke-Command command relies on PowerShell Remoting, so that must enabled, at least on the local machine.
Add a parameter block to the top of your "inner" script (the one you posted above), like so:
param (
[Parameter(Mandatory = $true)]
[string] $Path
)
That way, your "outer" script can pass in the directory path, using the -Path parameter for the "inner" script.

issue with scheduling a powershell script in task scheduler windows 2008 server

I am trying to run a powershell script on the task scheduler but am unable to understand the suitable logging command for my script. I want to get this to run on the schedule.
The script would delete the files and folders older than x days and would create an output log.
function Out-Log {
param (
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
[string]$message,
[switch]$Error
)
$logPath = $env:TEMP + "\Filedeletion.log"
$message | Out-File -FilePath $logPath -Append
}
trap
{
"error:" | Out-Log
$_.exception.message | Out-Log
break
}
$Error.Clear()
try
{
"Starting" | Out-Log
$DateToDelete = 1
$dateLimit = (Get-Date).AddDays(-$DateToDelete)
$StartFolder = "c:\TEST1"
Get-ChildItem -Recurse -Force -Path $StartFolder |
foreach {
$currentItemIsFolder = $_.PsIsContainer;
$curentItemIsOld = $_.LastWriteTime -lt $dateLimit
if ($curentItemIsOld -and (-not $currentItemIsFolder))
{
"Removing '$($_.fullname)'." | Out-Log
Remove-Item -Path ($_.fullname) -Force -WhatIf
}
}
}
finally
{
if ($Error)
{
"`$error stack:" | Out-Log
$error | foreach {$_.exception.ToString() | Out-Log}
}
"Stopping" | Out-Log
}
I was trying to use
Powershell -file "c:\Powershell\Filedeletion_logs_test.ps1"
via batch to run the powershell.
I've tried to check the commands in Powershell/? but did not find any suitable logging command working for my script.
Can anyone please help?
You do not need a separate batch file that runs the powershell in the task schedule. All you need is this nice tutorial. It is detailed step by step, but is very easy to setup.
http://community.spiceworks.com/how_to/show/17736-run-powershell-scripts-from-task-scheduler
Can't you do the same thing with SCHTASKS?
http://ss64.com/nt/schtasks.html
You should be able to pipe a list of servers through a TYPE command and add the tasks to the server set.
For example:
http://www.robvanderwoude.com/ntadmincommands.php
FOR /F %%A IN (servers.txt) DO (
SCHTASKS /CREATE /S %%A /U "system" /P "" /TN "Powershell Task" /TR "Powershell -file \"c:\my folder\script.ps1\""
)