Batch – Switch Proxy Auto-Config URL

This is a batch file I wrote designed for a user in a contracting environment to be able to easily switch between the corporate PAC and their customer’s PAC.  If the current PAC is set to the customer’s (or is blank), it will switch it to the corporate PAC; otherwise it will set the PAC to the customer’s.  Only the two variables at the top need be edited.

For reference, this is the setting in question:
pac

@echo OFF
rem -- Set proxy values in following two lines --
set OUR_COMPANY=http://proxy.example.com:8080/
set THEIR_COMPANY=http://proxy.customer.com:8080/
 
set KEY_NAME="HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
set VALUE_NAME=AutoConfigURL
 
FOR /F "usebackq skip=1 tokens=1-3" %%A IN (`REG QUERY %KEY_NAME% /v %VALUE_NAME% 2^>nul`) DO ( 
 set PROXY_VALUE=%%C
)
 
 
@echo Current proxy is %PROXY_VALUE%
 
IF "%PROXY_VALUE%"=="%OUR_COMPANY%" (
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v AutoConfigURL /t REG_SZ /d "%THEIR_COMPANY%" /f
echo Proxy switched to %THEIR_COMPANY%
) else (
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v AutoConfigURL /t REG_SZ /d "%OUR_COMPANY%" /f
echo Proxy switched to %OUR_COMPANY%
)
 
pause
Advertisements

Exchange – Undeliverable – 0x80070005-0x0004dc-0x000524

I use a few distribution groups with my Exchange Online-hosted personal email.  I delegate myself the ability to send as these groups so that I can send from other email addresses without having to have other mailboxes.

Recently, when when setting up a few distribution lists, I ran into this error when sending as them:

Your message did not reach some or all of the intended recipients.
Subject: test 123
Sent: 4/2/2016 8:09 PM
The following recipient(s) cannot be reached:
nonesuch on 4/2/2016 8:09
This message could not be sent. Try sending the message again later, or contact your network administrator. You do not have the permission to send the message on behalf of the specified user. Error is [0x80070005-0x0004dc-0x000524].

This was in spite of me having permission to do so. A quick Google search led to this Microsoft forum post.  At the very bottom user SteveLindsey states that this error occurs when the distribution lists are hidden.  Sure enough, when I unhid the distribution lists from the GAL, I was immediately able to send these emails.  SteveLindsey claims that this is a known issue by Microsoft, though I was unable to find a KB during my brief search.

At the time of this post, I am not certain if this is specific to Exchange Online/Office365 or Exchange 2013 in general.

Exchange Online – Bulk Import SMTP Aliases

I recently moved my personal email to Exchange Online.  I had been using SMTP aliases associated with my mailbox at my old provider, and wanted to continue doing so.

I was able to create a script to /u/life_manager and /u/RampageUT on this thread on Reddit.  Thanks to their scripts, I was able to create a script that was able to migrate a list of SMTP aliases to my new mailbox, without even having to install the Exchange admin tools on my machine.

The script is as follows.  The only changes that you need to make are to the array of SMTP aliases, and the mailbox name, both of which are at the top.

$aliasArray = @( # put SMTP aliases here
"example1@example.com",
"example2@example.com"
)
$mailbox = "username" # put mailbox ID here

$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session

$user = Get-Mailbox -Identity $mailbox

foreach ($alias in $aliasArray) {

 Set-Mailbox -Identity $user.Identity -emailaddresses @{add=$alias}

}

Remove-PSSession $Session

(PowerShell) Check For Workstation with no Logged on Users Based on Prefix

This is a PowerShell script designed to pull a list of workstations from AD based on a site code prefix, ping them, and check if anyone is logged on.  It will prompt if you want to connect via RDP when it finds one with nobody logged on, and will also prompt if you want to continue searching.  This requires psloggedon to be installed and in a folder in your PATH variable.

Function yesNo([string]$Title, [string]$Message) { # Simple message box function that prompts for Yes/No.
 
 $boolChoice = $false
 
 $objYes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "Yes"
 $objNo = New-Object System.Management.Automation.Host.ChoiceDescription "&No", "No"
 $objOptions = [System.Management.Automation.Host.ChoiceDescription[]]($objYes, $objNo)
 $intChoice=$host.ui.PromptForChoice($Title, $Message, $objOptions, 1)
 
 If ($intChoice -eq 0) { # 0 is Yes, 1 is No
 $boolChoice = $true
 }
 
 Return $boolChoice
 
}
 
Do { # Prompt for site code, prompt to ask again if no computers are found for site code.
 
 Clear-Host
 $boolRedo = $false
 $strSite = ""
 $strSite = Read-Host "Enter site code to search"
 Clear-Host
 Write-Host ("Checking for unused PCs at site " + $strSite + "...")
 Write-Host
 
 $colADComputers = @()
 $colADComputers = Get-ADComputer -Filter {name -like ($strSite + "-*")}
 
 If ($colADComputers -eq $Null) {
 $boolRedo = yesNo -Title "Try again?" –Message ("No PCs found for '" + $strSite + "', please check your site code. Would you like to try again?")
 }
 
} While ($boolRedo -eq $true)
 
foreach ($objComputer in $colADComputers) {
 
 $strComputer = $objComputer.Name
 
 If (Test-Connection -ComputerName $strComputer -Count 1 -Quiet) {
 $strOutput = psloggedon -x -l \\$strComputer 2>%err% | Out-String # Redirect stderr output to batch variable. The banner is output as stderr for some reason.
 $strOutput = $strOutput.ToString()
 $strOutput = $strOutput -replace ".*:" -replace "Connecting to Registry of \\\\" -replace $strComputer -replace "\.\.\." -replace "`n" -replace "[\s]{2,}",", " -replace "^, "
 $strOutput = $strOutput.Trim()
 If ($strOutput -like "No one is logged on locally.") {
 Write-Host $strComputer "is free." -ForegroundColor Green
 $boolConnect = yesNo -Title "Connect to PC?" -Message ("Free PC found: " + $strComputer + ". Connect via RDP?")
 If ($boolConnect) { mstsc.exe /v:$strComputer }
 $boolKeepSearching = yesNo -Title "Keep Searching?" -Message "Keep searching for free PCs?"
 If (!($boolKeepSearching)) { Break <# Exit the loop since we already found a free PC. #> }
 }
 Else {
 Write-Host $strOutput ("is logged on to " + $strComputer + ".") -ForegroundColor Yellow
 }
 }
 Else {
 Write-Host $strComputer "is down." -ForegroundColor Red
 }
 
}

(Batch) Check if Batch File is Running as Administrator (Windows 7/2008+ Compatible)

This code snippet will allow you to check if you are running with elevated permissions or not. Original credit goes to http://www.robvanderwoude.com/clevertricks.php.  However, the script there is not Windows 7 compatible as the “at” command is deprecated.  Here I use netstat with the “-b” parameter, which requires administrator privileges.

@echo off
netstat -a -n -b > nul
if %ERRORLEVEL% equ 0 (
	echo You are Administrator.
) else (
	echo You are NOT Administrator.
)
pause

Remove desktop.ini files from User Shares (Powershell)

This is a little script I wrote when I once had user Documents folders point to their user shares.  It made browsing the folders difficult, since they had all showed up as “Documents”.  I had since had folder redirection point to a subfolder, but the .ini files persisted.

$arrUsers = Get-ChildItem ("\\fileserver\users") | Where-Object {$_.PSIsContainer} | Foreach-Object {$_.FullName}
Foreach ($strUser in $arrUsers) {
	$strFile = $strUser + "\desktop.ini"
	Remove-Item -Path $strFile -Force
}