How can I use Invoke-Wuinstall silently - powershell

$WUInstallScript = { Import-Module C:\Path\PSWindowsUpdate.psm1; Get-WUInstall -AcceptAll -AutoReboot}
Invoke-WUInstall -ComputerName $hostname -Script $WUInstallScript
I am running this command but Invoke-WUInstall pops up
that whether I want to confirm this action.
I want to invoke this silently. Is there any option to do this?

Add -Confirm:$false switch like this:
Invoke-WUInstall -ComputerName $hostname -Script $WUInstallScript -Confirm:$false

I use a wu.ps1 file containing:
Import-Module PSWindowsUpdate
Invoke-Command {Get-WUInstall -AcceptAll -AutoReboot -Confirm:$FALSE}
and then something like:
powershell -file "C:\usr\wu.ps1"

Related

Log4J scanning fails to create csv file

invoke-command -scriptblock {powershell -File "D:\batch\Fix-4Log4J.ps1"} -computer $Server -AsJob -JobName "Scan4Log4J" -Verbose -EA SilentlyContinue
When I run this script locally, with enter-pssession the script works just fine but NO CSV
& D:\batch\Fix-4Log4J.ps1
Yet every time I try to invoke, directly or with SAPS it fails to create file
SYNTAX of EXE:
.\log4j2-scan.exe --force-fix d:\apps c:\users --report-csv --throttle 45
Now go and run it with NO other choices....
...and oh my goodness, I figured it out... :)
invoke-command -scriptblock {powershell -File "D:\batch\Fix-4Log4J.ps1"} -computer $Server -AsJob -JobName "Scan4Log4J" -Verbose -EA SilentlyContinue **get-job -name Scan4Log4J| receive-job -wait**
It was the receive-job that showed WHERE that BLASTED log went! HAH!
.\log\20220909.txt
WHEW!!!

Get-Item cmdlet on remote fileshare not returning result with invoke-command

I run a simple Get-Item cmdlet to get the below result :
(Get-Item -Path \\priam\coste\ZEDMB5T-Intransit-report\* -Include DAILYREPORT_TES_MSS_20190426_*.XLS).name
I try the same using an invoke-command, but it does not give me anything :
invoke-command -ScriptBlock {(Get-Item -Path \\priam\coste\ZEDMB5T-Intransit-report\* -Include DAILYREPORT_TES_MSS_20190426_*.XLS).name} -computername localhost -Credential Get-Credential
There is nothing wrong with the credentials since it does work for the below command :
invoke-command -ScriptBlock {"testing"} -computername localhost -Credential Get-Credential
I have access to the path as well :
Get-Acl -Path \\priam\coste\ZEDMB5T-Intransit-report | Format-List -Property *
I am running the powershell ISE as an administrator, as seen in the title bar :
I am using a recent version of PS :
What am i doing wrong here?

How to write local variable to a file in remote server using Powershell script?

On a remote server there is a .BAT file which uses a .properties file to run.
I am able to run the .BAT file calling the .properties file, but in that .properties file last line is:
exportQuery1=SELECT * FROM CI_INFOOBJECTS where SI_ID='123456'.
I am modifying that line/SI_ID value manually which actually increasing my effort.
I have tried a few options but am not able to provide the value/entire line from the local powershell commandline which will be written in the .properties file.
So I have to modify the .ps1 every time. I want to pass the entry with the local powershell command as a variable.
Deleting the old line:
Invoke-Command -computername $ServerName -Credential $Cred -ErrorAction stop -ScriptBlock {Set-Content -Path D:\Script\TestFile.txt -Value (get-content -Path D:\Script\TestFile.txt | Select-String -Pattern 'SI_ID' -NotMatch)}
Creating the New line at the end of the file:
Invoke-Command -computername $ServerName -Credential $Cred -ErrorAction stop -ScriptBlock {add-content D:\Script\TestFile.txt "exportQuery1=SELECT * FROM CI_INFOOBJECTS where SI_ID='abcdef'"}
Please help to pass the SI_ID/entire line from the command while executing the script.
Why not use a simple parameter and the using statement in a single invoke call?
param($SI_ID)
$SB = {
Set-Content -Path D:\Script\TestFile.txt -Value (get-content -Path D:\Script\TestFile.txt | Select-String -Pattern 'SI_ID' -NotMatch)
add-content D:\Script\TestFile.txt "exportQuery1=SELECT * FROM CI_INFOOBJECTS where SI_ID='$using:SI_ID'"
}
Invoke-Command -computername $ServerName -Credential $Cred -ErrorAction stop -ScriptBlock $SB
then just .\myscript -SI_ID "abcd"

How to get all (IIS) websites status/state from remote machine using PowerShell?

I was trying to read the status of IIS website from remotely. I tried below code.
$Session = New-PSSession -ComputerName $serverName
$block = {
import-module 'webAdministration'
Get-ChildItem -path IIS:\Sites
}
Invoke-Command -Session $Session -ScriptBlock $block | Out-File -append $WebreportPath
But above command given me only Websites with https binding, when it comes to https it throws below error.
The data is invalid. (Exception from HRESULT: 0x8007000D)
+ CategoryInfo : NotSpecified: (:) [Get-ChildItem], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException,Microsoft.PowerShell.Commands.GetChildItemCommand
I am trying to renmote a Windows Server 2008 R2 with IIS 7.
Please guide me. Thanks !
Hope this will help for someone. Below is my findings and the answer.
Powershell sends only objects remotely, and render them using available templates.
So need to give a template accordingly. I used "Format-Table".
Be-careful, if you use "Format-Table" outside it results same error. Remember to use it inside the scriptblock.
Error-prone:
Invoke-Command -ComputerName $serverName { Import-Module WebAdministration; Get-ChildItem -path IIS:\Sites} | Format-Table | Out-File -append $WebreportPath
Correct code:
Invoke-Command -ComputerName $serverName { Import-Module WebAdministration; Get-ChildItem -path IIS:\Sites | Format-Table} | Out-File -append $WebreportPath
My refers :
http://forums.iis.net/t/1155059.aspx?IIS+provider+and+PowerShell+remoting
Good Luck !
There seems to be some problem with retrieving the objects using a remote session, but that is no blocking issue (I have run into this as well).
To prevent the error from being written, you can use -ErrorAction like this:
$Session = New-PSSession -ComputerName $serverName
$block = {
Import-Module 'webAdministration'
Get-ChildItem -path IIS:\Sites
}
Invoke-Command -Session $Session -ErrorAction SilentlyContinue -ScriptBlock $block |
Out-File -append $WebreportPath
This way you will not see the remoting error, but you will still receive the data you need.
Note that this hides all errors, so you are possibly hiding an error that you may want to see. For that you will need a fix for the underlying ptoblem, so you can remove the -ErrorAction patch.

PowerShell Command to Copy File on Remote Machine

I have a requirement to copy file from local machine to remote machine using PowerShell. I can copy the file to remote computer using following command:
copy-item -Path d:\Shared\test.txt -Destination \\server1\Shared
the above command uses network share path to copy the file. I don't want to use network share option as the folder will not be shared on the remote machine. I tried following commands but not working.
copy-item -Path d:\Shared\test.txt -Destination \\server1\c$\Shared
Invoke-Command -ComputerName \\server -ScriptBlock {
copy-item -Path D:\Shared\test.txt -Destination C:\Shared
}
Please let me know how to make it working without using UNC path. I have full permissions on that folder on the remote machine.
Quickest way I found to this, since the account being used is Administrator, is to do the following:
New-PSDrive -Name X -PSProvider FileSystem -Root \\MyRemoteServer\c$\My\Folder\Somewhere\
cd X:\
cp ~\Desktop\MyFile.txt .\
## Important, need to exit out of X:\ for unmouting share
cd c:\
Remove-PSDrive X
Works every time.
You must have a shared folder to be able to copy files from one host to another, either on the remote host if you want to push the file:
Copy-Item -Path D:\folder\test.txt -Destination \\server1\remoteshare\
or on the local host if you want to pull the file:
Invoke-Command -ComputerName server1 -ScriptBlock {
Copy-Item -Path \\localcomputer\localshare\test.txt -Destination C:\Shared\
}
Administrative shares (\\server1\c$) can only be used if your account has admin privileges on that particular computer.
If there is not an accessible share, you'll have to make the file content itself an argument to the script:
Invoke-Command -ComputerName \\server -ScriptBlock {
$args[0] | Set-Content C:\Shared\test.txt
} -ArgumentList (Get-Content D:\Shared\test.txt -Raw)
Powershell 5 (Windows Server 2016)
Also downloadable for earlier versions of Windows. -ToSession can also be used.
$b = New-PSSession B
Copy-Item -FromSession $b C:\Programs\temp\test.txt -Destination C:\Programs\temp\test.txt
Earlier versions of PowerShell
Does not require anything special, these hidden shares exist on all machines.
Copy-Item -Path \\serverb\c$\programs\temp\test.txt -Destination \\servera\c$\programs\temp\test.txt
Invoke-Command -ComputerName compname -Credential $cred -ScriptBlock { Get-Content C:\myfolder\result.txt } >>res.txt
Note the C:\myfolder\result.txt is on the remote computer
Here's a script that worked for me for small files. Run as admin.
#pre 5.0 powershell copy-item to remote computer
Write-Host "Remote copy a file"
$servers = #("server01.dot.com", "server02.dot.com")
foreach($server in $servers) {
$username = 'USERNAME'
$password = 'PASSWORD'
$pw = ConvertTo-SecureString $password -AsPlainText -Force
$cred = New-Object Management.Automation.PSCredential ($username, $pw)
$myfile = [System.IO.File]::ReadAllBytes("C:\Temp\srctest.txt")
$s = New-PSSession -computerName $server -credential $cred
Enter-PSSession $s
Invoke-Command -Session $s -ArgumentList $myfile -Scriptblock {[System.IO.File]::WriteAllBytes("C:\Temp\desttest.txt", $args)}
Write-Host "Completed"
Remove-PSSession $s
}