Get-SPOSite find existing sites - powershell

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

Related

How to change Custom Object Property Values while doing foreach-object loop on Objectarray

function DeleteADUser ()
{
$AccountObjectCollection | foreach-object {
if ($_.ADAccountExists -eq $true)
{
write-host "User: $($_.ADAccount) found. Deleting..." -ForegroundColor white
$User = Get-ADUser -server $domain -Identity $_.ADAccount
Remove-ADUser $User -server $domain -Confirm:$False
write-host "User: $($_.ADAccount) deleted. Check again..." -ForegroundColor white
try {
Get-ADUser -server $domain -Identity $_.ADAccount | Out-Null
write-host "User: $($_.ADAccount) exists. Deletion failed!." -ForegroundColor red
}
catch [Microsoft.ActiveDirectory.Management.ADIdentityResolutionException] {
$_.ADAccountExists = $false
write-host "User: $($_.ADAccount) deleted." -ForegroundColor green
}
}else {write-host "User: $($_.ADAccount) not found." -ForegroundColor green}
}
}
I have some custom objects in an object array $AccountObjectCollection and try to changes values in my foreach loop on the fly but the line
$_.ADAccountExists = $false gets the following exception:
´The property 'ADAccountExists' cannot be found on this object. Verify that the property exists and can be set.´ Thats odd because I'm literally in an IF Statement that only shoots when this specific Value exists and is set to true so I'm propably doing something wrong here. How can I change the value inside the foreach loop?
Inside your catch block, $_ refers to an exception object, not the current AD account object piped into your foreach-object loop.
Thus, it might be best if you used:
foreach ($ADObject in $AccountObjectCollection) {
# ...
}
...and replaced your instances of $_ inside this foreach block with $ADObject

Powershell: Fix My Loop Logic

I am trying to get the logic straightened out for a loop that I need to continue to prompt a user to enter a valid UNC path. I have it set to test the path and output to the console that the path is invalid. But, after that, it moves back to my prompt for choice. I want it to instead, ask the user to enter another valid path before moving on to the next step. Here is my loop:
do{
Write-Host ""
$pathPrompt = Write-Host "Please enter path to file/folder:" -ForegroundColor Cyan;
$path = Read-Host;
$test = Test-Path $path
if($test -eq $false){
Write-Host "ERROR! Invalid Path!" -ForegroundColor Red
}
}until($test -eq $true){
Write-Host ""
Write-Host "Getting ACL on"$path -ForegroundColor Green
Get-NTFSAccess -Path $path
}
What am I missing or not doing right here?
Sounds like you want to reuse your validation test. You could put it in a function for reuse:
Function Get-ValidPath {
do {
Write-Host "`r`nPlease enter path to file/folder:" -ForegroundColor Cyan
$path = Read-Host
$test = Test-Path $path
if ($test -eq $false) {
Write-Host "ERROR! Invalid Path!" -ForegroundColor Red
}
} until ($test -eq $true)
$path
}
$validatedpath1 = Get-ValidPath
Write-Host "`r`nGetting ACL on $validatedpath1" -ForegroundColor Green
Get-NTFSAccess -Path $validatedpath1
$validatedpath2 = Get-ValidPath

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 - verify object exists in AD

i have a text file containing arround 100 servers, how can i push these into a script and test if they exist within AD? I have a simple script below:
$serverlist = get-content ServerList.txt
foreach ($server in $serverlist) {
if (Get-ADComputer $serverlist ) {
Write-Host "#########################"
Write-Host "Computer object exists"
Write-Host "#########################"
}
else {
Write-Host "#########################"
Write-Host "Computer object NOT FOUND"
Write-Host "#########################"
}
}
the above does not work returning a error:
Get-ADComputer : Cannot convert 'System.Object[]' to the type 'Microsoft.ActiveDirectory.Management.ADComputer' required by parameter 'Identity'. Specified method is not supported.
Can someone please explain does the get-adcomputer only allow a single object? Also if i remove the txt file and add a server shown below:
if (Get-ADComputer "server name" )
The above provides only results if the server exists within AD, if the server does not the error is shown below:
Get-ADComputer : Cannot find an object with identity: 'iuiub' under: 'DC=####,DC=#####,DC=#####'
Thank you for any insight / help!
Phil
Create an array - #(). If the array has 1 or more objects in it - which is $true - then you know the computer exists. If the array has 0 objects in it - which is $false- then you know the computer doesn't exist. I know some people don't like the ErrorAction to be set to SilentlyContinue but you're "Outputting an Error" if an error does occur.
$serverlist = get-content ServerList.txt
foreach ($server in $serverlist) {
if (#(Get-ADComputer $server -ErrorAction SilentlyContinue).Count) {
Write-Host "#########################"
Write-Host "Computer object exists"
Write-Host "#########################"
}
else {
Write-Host "#########################"
Write-Host "Computer object NOT FOUND"
Write-Host "#########################"
}
}
Another thing you could try are try catch blocks. Sorta like this:
$serverlist = get-content ServerList.txt
foreach ($server in $serverlist) {
try{
Get-ADComputer $server -ErrorAction Stop
Write-Host "#########################"
Write-Host "Computer object exists"
Write-Host "#########################"
}
catch{
Write-Host "#########################"
Write-Host "Computer object NOT FOUND"
Write-Host "#########################"
}
}
Line 3, change $serverlist to $server
With regards to handling a not found result. I'd try flipping the logic :
$serverlist = get-content ServerList.txt
foreach ($server in $serverlist) {
$tempVar = Get-ADComputer $server
if ($tempVar -like "Get-ADComputer : Cannot find an object with identity" ) {
Write-Host "#########################"
Write-Host "Computer object NOT FOUND"
Write-Host "#########################"
}
else{
Write-Host "#########################"
Write-Host "Computer object exists"
Write-Host "#########################"
}
}
In order to get a more helpful output, I'd go with the following... You'll just have a list of green and red lines indicating which server was found and which one wasn't.
$serverlist = get-content ServerList.txt
foreach ($server in $serverlist) {
try {
Get-ADComputer $server -ErrorAction Stop | Out-Null
Write-Host "$($server) exists" -ForegroundColor DarkGreen
}
catch {
Write-Host "$($server) NOT FOUND" -ForegroundColor DarkRed
}
}

get-hotfix on single server

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