get-hotfix on single server - powershell

first time using powershell and I can't seem to get the below to work - nothing is displayed - I think the script work but I assume I need something to show the results? Any help please:
$hotfix1 = Get-HotFix -Id KB981872
If($hotfix)
{
$Output = "Hotfix is installed you may proceed"
}
else
{
$Output = "Hotfix is not installed"
}
$hotfix1 = Get-HotFix -Id KB981872
Thanks Shay - I have updated it as:
write-host "This will check if Hotfix KB979808 is installed on this Server." -ForegroundColor
Black -BackgroundColor Cyan
write-host "This is required for Windows Server 2008 R2 DFSR Pre-Seeding Robocopying" -
ForegroundColor Black -BackgroundColor Cyan
Write-Host ""
$hotfix1 = Get-HotFix -Id KB979808 -ErrorAction SilentlyContinue
If($hotfix1 -match "KB979808")
{
Write-Host "Hotfix is installed you may proceed" -foregroundcolor "green"
Write-Host ""
}
else
{
Write-Host "Hotfix is NOT installed - Please ensure you install this hotfix BEFORE"
Write-host "copying any data" -foregroundcolor "red"
Write-Host ""
}

The code doesn't output anything because you assign it to a variable. Remove the assignment. You are also assigning the command output to $hotfix1 but checking against $hotfix in the if statement. In addition, if the hot-fix cannot be found you'll get an error, so add the -ErrorAction parameter to suppress the error:
$hotfix1 = Get-HotFix -Id KB981872 -ErrorAction SilentlyContinue
If($hotfix1)
{
"Hotfix is installed you may proceed"
}
else
{
"Hotfix is not installed"
}
$hotfix1

Related

Powershell not reading .txt lines

I cannot get PowerShell to run this .txt file, what am I doing wrong? I tried changing the name of the .txt and checked passed scripts and everything seems to be the same but I keep getting an error saying that the ".txt in invalid"
$POSName = "$PSScriptRoot\Bex.txt"
foreach ($POS in (Get-Content $POSName)) {
$Bex = Get-Service -ComputerName $POSName | Where-Object { $_.name -eq "BexServ" }
}
If ($Bex -eq $null) {
# Service does not exist
Write-Host " doesn't exist." -ForegroundColor Red
}
Else {
# Service does exist
Write-Host "The $($Bex.Name) service found." -ForegroundColor Green
If ($Bex.Status -eq "Running") {
# Stop Service
Set-Service -status stopped -ComputerName $POSName -name $Box.Name -ErrorAction Stop
Write-Host "The $($Bex.Name) successfully stopped." -ForegroundColor Green
}
else {
#service already stopped
If ($Bex.Status -eq "Stopped") {
Write-Host "The $($Bex.Name) service already Stopped." -ForegroundColor Green
}
}
}
As commented, you are using the wrong variable in the loop. The code is reading the text file just fine, it is Get-Service that cannot deal with a path to a file in the -ComputerName parameter.
Also, the placing of the if..else should be inside the loop, not after.
Try
$POSName = "$PSScriptRoot\Bex.txt"
foreach ($POS in (Get-Content $POSName)) {
$Bex = Get-Service -ComputerName $POS | Where-Object { $_.name -eq "BexServ" }
If (!$Bex) {
# Service does not exist
Write-Host " doesn't exist." -ForegroundColor Red
}
Else {
# Service does exist
Write-Host "The $($Bex.Name) service found." -ForegroundColor Green
If ($Bex.Status -eq "Running") {
# Stop Service
Set-Service -status stopped -ComputerName $POSName -name $Box.Name -ErrorAction Stop
Write-Host "The $($Bex.Name) successfully stopped." -ForegroundColor Green
}
else {
#service already stopped
If ($Bex.Status -eq "Stopped") {
Write-Host "The $($Bex.Name) service already Stopped." -ForegroundColor Green
}
}
}
}
It might also be a good idea to output the computername ($POS) in the Write-Host lines too

Get-SPOSite find existing sites

This is the code that will check if any of the sites listed already exists.
$sites = get-content -Path C:\code\CheckSPSites.txt
foreach($site in $sites){
$url = (Get-SPOSite -Filter{url -like $site} -ErrorAction SilentlyContinue) -ne $null
if ($url -eq $true){
Write-Host "$site already created" -BackgroundColor Red
}else{
Write-Host "$site not created" -BackgroundColor Green
}
}
It doesn't find the sites when I use the variable $site to filter the search.
I've tried putting the variable in quotes (Get-SPOSite -Filter{url -like "$site") and it doesn't work either.
Any help would be appreciated.
Many thanks
Thank you Theo and UBK. Posting your suggestion as an answer to help other community members.
You can use -Identity parameter along with the site collection URL to find out the existence.
For example: Get-SPOSite -Identity https://contoso.sharepoint.com
# Verify if site of same name already exists in SharePoint Online
$siteAlreadyExists = Get-SPOSite -Identity $url
# If it does, stop the script
if ($null -ne $siteAlreadyExists) {
Write-Host "Site already exists" -ForegroundColor Red
...
}
If your code fails when site is not present, then you can try following try-catch block:
try {
$siteAlreadyExists = Get-SPOSite -Identity $url
if ($null -ne $siteAlreadyExists) {
Write-Host "Site already exists" -ForegroundColor Red
}
}
catch {
Write-Host "Site already exists" -ForegroundColor Red
}
You can refer to Number of SPO sites causing issue with Get-SPOSite

Powershell if else condition ignore warning keep to continue

I would like to design a condition which is if the tcp connection result is return "true", will proceed install action and go next function. Otherwise, it will return a warning message, stop to proceed and pass to next function. I don't know how to implement keep to proceed next function if occurs any warning on current function.
Is there anybody can help to fix the issue?
Many Thanks.
function A(){
$printerIPAddress = "192.168.1.100"
$sharedPrinter = "\\SERVERNAME\A"
$checkPrinterExists = Get-Printer -Name $sharedPrinter -ErrorAction SilentlyContinue
if ((Test-NetConnection -ComputerName $printerIPAddress -WarningAction SilentlyContinue).PingSucceeded){
Write-Host "Succeed to connect - $printerIPAddress" -ForegroundColor Black -BackgroundColor Gray
Start-Sleep(2)
}
else {
Write-Warning "Failure to connect - $printerIPAddress"
}
if (-not $checkPrinterExists){
Write-Host "Installing a printer..."
Add-Printer -ConnectionName $sharedPrinter
Write-Host "Succeed to install - A Printer" -ForegroundColor Black -BackgroundColor Gray
}
else{
Write-Warning "Failure to install - A Printer already exist"
}
}
function B(){
$printerIPAddress = "192.168.1.101"
$sharedPrinter = "\\SERVERNAME\B"
$checkPrinterExists = Get-Printer -Name $sharedPrinter -ErrorAction SilentlyContinue
if ((Test-NetConnection -ComputerName $printerIPAddress -WarningAction SilentlyContinue).PingSucceeded){
Write-Host "Succeed to connect - $printerIPAddress" -ForegroundColor Black -BackgroundColor Gray
Start-Sleep(2)
}
else {
Write-Warning "Failure to connect - $printerIPAddress"
}
if (-not $checkPrinterExists){
Write-Host "Installing a printer..."
Add-Printer -ConnectionName $sharedPrinter
Write-Host "Succeed to install - B Printer" -ForegroundColor Black -BackgroundColor Gray
}
else{
Write-Warning "Failure to install - B Printer already exist"
}
}
Write-Host "Running...Please wait..."
A;
B;
I've attempted to simplify your code and also inherently handled the "stop to proceed if Test-NetConnection fails" scenario.
As your main objective is to install the printer, the function will error out if the parameter $sharedPrinter fails the defined ValidateScript criteria.
Function Install-Printer {
[CmdletBinding()]
Param(
[parameter(Mandatory=$true)]
[ValidateScript({$_ -match [IPAddress]$_ })]
[string]
$printerIPAddress,
[parameter(Mandatory=$true)]
[ValidateScript({(Test-Path $_) -and ($Printer = Get-Printer -Name $_)})]
[string]
$sharedPrinter
)
if ((Test-NetConnection -ComputerName $printerIPAddress -WarningAction SilentlyContinue).PingSucceeded) {
Write-Host "Succeed to connect - $printerIPAddress" -ForegroundColor Black -BackgroundColor Gray
Write-Host "Installing a printer..."
Add-Printer -ConnectionName $sharedPrinter
if($?){ # if previous command succeeded
Write-Host "Succeed to install - $($Printer.Name) Printer" -ForegroundColor Black -BackgroundColor Gray
}
else {
Write-Warning "Failure to install - $($Printer.Name) Printer already exist"
}
}
else {
Write-Error "Failure to connect - $printerIPAddress"
}
}
Write-Host "Running...Please wait..."
# Install Printer A
Install-Printer -printerIPAddress "192.168.1.100" -sharedPrinter "\\SERVERNAME\A"
# Install Printer B
Install-Printer -printerIPAddress "192.168.1.101" -sharedPrinter "\\SERVERNAME\B"

IF/ELSE Get-SPWeb

I am trying to quickly check whether a web site exists. I seem to have an error in my IF statement, but I'm not sure of the correct syntax. Here's my code:
$URLis = "https://ourdevsite.dev.com/sites/flibidyboots"
add-pssnapin microsoft.sharepoint.powershell -ea 0
IF ((Get-SPWeb $URLis) -ne 0){
Write-Host "Site does not exist, so we can proceed with building it" -foregroundcolor green
}
Else {
Write-Host "Site does exist, so we need to pick another URL" -foregroundcolor red
}
What did I get wrong?
Ok, first of all its $null, not 0. And second, if it is not $null it exists, so your cases are mixed up.
Here is some code that will work.
Add-PSSnapin "Microsoft.SharePoint.Powershell" -ErrorAction SilentlyContinue
$url = "http://teams"
$w = Get-SPWeb -Identity $url -ErrorAction SilentlyContinue
if ($w) {
Write-Host "Site Exists" -ForegroundColor Green
} else {
Write-Host "No Site" -ForegroundColor Red
}

PowerShell test-connection, if service exists using get-service

Basically I want to check and see if the computers in the text file are online. If they aren't online then write-host "$computer is down". If the $computer is online then check to see if this service exists, if it exists then write-host "$computer installed, if not then write-host "$computer not installed". The Test-connection seems to work but if the computer is online they all return write-host "$computer installed" even though I have a test machine that I know doesn't have this service running.
function Get-RunService {
$service = get-service -name ABCService
Get-Content "C:\powershell\computers.txt" |
foreach {if (-not (Test-Connection -comp $_ -quiet))
{
Write-host "$_ is down" -ForegroundColor Red
}
if ($service )
{
write-host "$_ Installed"
}
else {
Write-host "$_ Not Installed"
}
}
}
get-RunService
Have a look at this cleaned up version of your code.
function Get-RunService {
Get-Content "C:\powershell\computers.txt" |
foreach {
if (-not (Test-Connection -comp $_ -quiet)){
Write-host "$_ is down" -ForegroundColor Red
} Else {
$service = get-service -name ABCService -ComputerName $_ -ErrorAction SilentlyContinue
if ($service ){
write-host "$_ Installed"
} else {
Write-host "$_ Not Installed"
}
}
}
}
get-RunService
I tried to clean up how the brackets were working. Your check if the host was alive did not have an Else to separate the case off the server being contactable or not. Side note is that ping could fail but the host could still be alive and that all depends on your environment but be aware of the possibility. Also moved the $service line into the foreach adding the -ComputerName $_
Currently you have no margin for error with this. That function is possible to not exist and you should account for that. Best advice would be to look into -ErrorAction of Get-Service and possibly a Try/Catch block.
It's been a while, but I think this version is a bit more clear. Why check for it to be offline instead of only performing the actions if the computer is online.
function Get-RunService {
Get-Content "C:\powershell\computers.txt" |
foreach
{
if (Test-Connection -comp $_ -quiet)
{
$service = get-service -name ABCService -ComputerName $_ -ErrorAction SilentlyContinue
if ($service ) { Write-Host "$_ Installed" }
else { Write-Host "$_ Not Installed" }
}
else
{ Write-Host "$_ is offline!" -ForegroundColor Red }
}
}