Cool test to ensure YOURLS is working

I installed YOURLS on another domain so I can use short URL’s on the blog. This is a test to see if it works. The following link didn’t get generated correctly because of HostGator and their strict security settings. I had to chat with them for over 30 minutes and provide this google link to the problem https://code.google.com/p/yourls/issues/detail?id=407 to have them resolve it. I’ve only heard that Hostgator is the only offending host that blocks this.

Beh! Still didn’t work! Looking into why 🙁

Eventually gave up because I think it’s Hostgator. Looking for a new hosting provider

EddieRock

Connect remotely to a computer with powershell and get ipconfig /all from it

Are you looking to connect remotely to a computer and run powershell commands. Below is a simple one liner that will connect to a computer and get the ipconfig /all and display it on your computer.

If it doesn’t work, you may have to enable WinRM on the remote computer. This can be done with group policy or simply typing Winrm quickconfig at the command prompt on the computer you want to connect to. This has to be done before hand so there may be some prep involved but when you have Winrm enabled on all your computers, you can connect with powershell and run all sorts of commands.

Here is

Invoke-Command -Computer <EnterComputerNameHere> -ScriptBlock {ipconfig /all} 

How to Get The Logged On User on a Remote Windows Machine

This one is super cool and you will amaze your boss. Many times, I’m asked if I can find who is logged into a machine.

As long as you have PSRemoting enabled across your network you can run this:

Also replace IP or name with the IP address or computer name of the computer

get-wmiobject win32_computersystem -computer IP or name | select username

or you can do a | select * and it will give you more info.

How to Search Files Recursivly and Replace content in files

I was tasked to search many files in a directory structure and find content in each file and replace that content. The directory has MANY types of files in it so I had to search for a certain type of file, then search inside that file and replace a string if found.

So, I did this:

Get-ChildItem | ForEach-Object Get-Content -replace and then Set-Content

change *File.Type* with the filetype you want to search for. It could be a *.log or a *.bak

Change string1 to what you’re looking for and string2 to what you want to change it to.

NOTE: This code will save the file with a new date even if it doesn’t replace the string. One minor issue that you must be aware of because if you later want to search for all old files and delete them, it wont find old ones that you’ve touched with this script because of the date change. I’m working on a new version that only saves the file if the string is found in it. Feel free to comment if you got the answer to this 🙂

Here is the code:

Get-ChildItem -include *File.Type* -Recurse | ForEach-Object { ( Get-Content -Path $_.FullName ) -replace 'string1', 'string2' | set-content $_.fullname }

Script to get a distribution list membership easily

Throughout my years as a System Administrator, I’ve been asked repeatedly to produce a list for someone of the contents of a distribution list. I searched and found some scripts out there and ended up modifying one that now fits my needs.

# Gets distribution list members from a group in exchange
# Author: Ed Rockwell
# Company: Some Company You Work At
# Date: Today :)
# Ver: 1.0
 
param (
	$OutputFile = "C:\listmembers.txt"
	)
 
Write-Host "Checking if output file already exists..." -NoNewline
 
if (Test-Path $OutputFile)
	{
		Write-Host "Found old output file from last run!"
		Write-Host "Removing old output file..." -NoNewline
		Remove-Item $OutputFile -Force
		Write-Host "Done!"
	}
else
	{
		Write-Host "Cleanup of last output file not found... Good!"
	}
 
IF ( -NOT ( Get-PSSnapin -Name "Microsoft.Exchange.Management.PowerShell.E2010" -ErrorAction SilentlyContinue )) { Add-PSSnapin "Microsoft.Exchange.Management.PowerShell.E2010" }
 
$UserCredential = Get-Credential "Domain\User"
$saveto = "C:\listmembers.txt"
$UserEntryGroupName = Read-Host "Enter the Group you would like to get the members"
 
Get-DistributionGroup -identity "$UserEntryGroupName" | sort name | ForEach-Object {
		"`r`n$($_.Name)`r`n=============" | Add-Content $saveto
		Get-DistributionGroupMember $_.Name | sort Name | ForEach-Object {
					$_.Name + " <" + $_.PrimarySMTPAddress + ">" | Add-Content $saveto
				}
		}
 
Notepad c:\listmembers.txt

Move a Virtual Machine from one host to another

After building a new set of VMware ESX servers in a new vCenter cluster, I was tasked with moving around 200 virtual servers from the old cluster to the new cluster. To do this in an orderly fashion and to make sure I didn’t miss a step, I turned to Powershell to help me accomplish the task. The below script does the following:

  • Validates that you’re logged in using an admin account. Hopefully, you are and your naming convention for all your admin accounts is the same. Simply edit that part to match your convention or comment it out
  • You then Enter the server name and it fetches the Cluster, Host and Server information from VMWare.
  • The script then queries VMware vSphere for the clusters available and puts them in a list for selection.
  • You are then presented with a list of hosts in that cluster to select the destination server to move the virtual server to.
  • Then it allows you to review what you’re going to do before any servers are shutdown, moved, VMware tools upgrade and and final reboot.
  • Sometimes the script gives some errors at the end as it has a hard time determining if the server is back online. Not a big deal and everything works great!
$starttime = get-date
 
#Region Validate Admin Account
Clear-Host
Write-Host "Validating this is running as an `"Admin`" account."
# If you use a naming convention in your administrative accounts, change this next line to check for that convention
IF ($ENV:USERNAME -notlike "*ADMIN") { THROW "SCRIPT MUST BE RUN WITH ADMIN ACCOUNT." }
 
Write-Host "Good Job! You're using your ADMIN account!!"
 
#EndRegion
 
#Region Gather Migration Information
 
#Region Connecting to VMWare
Write-Host "Adding VMWare Snapin."
Write-Host ""
 
IF ( -NOT ( Get-PSSnapin -Name "VMware.VimAutomation.Core" -ErrorAction SilentlyContinue )) { Add-PSSnapin "VMware.VimAutomation.Core" }
TRY { Disconnect-VIServer -Server * -Force -Confirm:$FALSE -ErrorAction SilentlyContinue } CATCH { }
# Put in your FQDN of your virtual center server below
Connect-VIServer -Server FQDN.OF.VIRTUALCENTER -WarningAction SilentlyContinue | Out-Null
 
#EndRegion
 
#Region Get Server Info
# Search for server to move
$UserEntryVM = Read-Host "Enter the Virtual Machine you would like to move"
IF ( -NOT (Get-VM $UserEntryVM)) { THROW "Could not find $UserEntryVM." }
$VMToMove = Get-VM $UserEntryVM
 
#EndRegion
 
#Region Source Cluster And Host
# Get Cluster and VMHost server is on
 
$SourceCluster = $VMToMove.VMHost.Parent
$SourceHost = $VMToMove.VMHost
Write-Host
Write-Host "The Details of the Source machine are:"
Write-Host
Write-Host "Server to Move:`t`t$VMToMove"
Write-Host "Source Cluster Name:`t$SourceCluster"
Write-Host "Source Host:`t`t$SourceHost"
Write-Host ""
$UserResponse = Read-Host "Is This Informaiton Correct? (y/n)"
IF ($UserResponse.length -ne 0) {
IF ("y","Y" -NotContains $UserResponse) { THROW "INVALID ENV."}
}
 
#EndRegion
 
#Region Destination Cluster And Host
# Gather Destination VMWare Host Information
Write-Host
Write-Host "Which Cluster would you like to move $VMToMove to?"
$ESXClusters = (Get-Cluster)
 
$i = 0
FOREACH ( $Cluster IN $ESXClusters )
{
$i++
write-host "`t$i) $Cluster"
 
}
Write-Host ""
[int]$UserInput = Read-Host "Please make selection above (1 - $i)"
$ChosenCluster = $ESXClusters[($UserInput-1)]
 
$ClusterHosts = ( Get-VMHost -State Connected -Location "$ChosenCluster" )
Write-Host
Write-Host "This list does not list hosts in Maintenance Mode"
Write-Host "Which Host would you like to move $VMToMove to?"
$i = 0
FOREACH ( $ESXHost IN $ClusterHosts )
{
$i++
write-host "`t$i) $ESXHost"
 
}
Write-Host ""
[int]$UserInput = Read-Host "Please make selection above (1 - $i)"
$ChosenHost = $ClusterHosts[($UserInput-1)]
 
#EndRegion
 
#Region Validate Input
 
# Clear-Host
Write-Host ""
Write-Host "Below is what you want to do:"
Write-Host "-----------------------------"
Write-Host "Server To Move:`t`t$VMToMove"
Write-Host "Source Cluster:`t`t$SourceCluster"
Write-Host "Source Host Name:`t$SourceHost"
Write-Host ""
Write-Host "The Details of Destination ESX Host are:"
Write-Host "----------------------------------------"
Write-Host "Destination Cluster:`t$ChosenCluster"
Write-Host "Destination Host:`t$ChosenHost"
Write-Host ""
$UserMoveResponse = Read-Host "IS ALL THIS INFORMATION CORRECT? (y/n)"
IF ($UserMoveResponse.length -ne 0) {
IF ("y","Y" -NotContains $UserMoveResponse) { THROW "INVALID ENV."}
}
 
#EndRegion
 
#EndRegion
 
#Region Moving Server, Reboot, Update VMWare Tools
 
# Shutdown VM
Write-Host "Shutting Down $VMToMove"
Shutdown-VMGuest $VMToMove -Confirm:$false
WHILE ( $VMState = get-vm $VMToMove | WHERE {$_.PowerState -ne "PoweredOff"}) {start-sleep -Seconds 5}
Write-Host "$VMToMove is now off, beginning move from $SourceHost to $ChosenHost"
 
# Move VM
Get-VM $VMToMove | Move-VM -destination (Get-VMHost "$ChosenHost") -Confirm:$false
 
# Validate the move
 
# Power On VM
start-vm $VMToMove -Confirm:$false
WHILE ( $VMState = get-vm $VMToMove | WHERE {$_.PowerState -ne "PoweredOn"}) {start-sleep -Seconds 5}
WHILE ( Get-Task | WHERE {$_.Name -eq "PowerOnVM_Task" -and $_.State -ne "Success" } ) { Start-Sleep -Seconds 5 ; Get-VMQuestion | Set-VMQuestion –Default -Confirm:$False }
Write-Host "$VMToMove is powering up. This should take a minute"
 
# update vmware tools
WHILE ( -NOT ( test-connection $VMToMove -quiet ) ) {start-sleep -Seconds 5}
DO {start-sleep -Seconds 45} WHILE ( get-service -ComputerName $VMToMove -name VMTools | WHERE {$_.status -ne "running"})
Start-Sleep -Seconds 15
Write-Host "Server is online, trying to update tools..."
Update-Tools $VMToMove
#EndRegion
Write-Host "Server is rebooting after VMWare Tools Install..."
DO {start-sleep -Seconds 15} WHILE (!( test-connection $VMToMove ))
$TimeSpan = New-TimeSpan -Start $starttime
Write-Host "Server should be up..."
Write-Host ""
Write-Host "Pretty cool... Huh? This move took $TimeSpan.Minutes,$TimeSpan.Seconds"
Write-Host ""
Write-Host "You're Done! Please VALIDATE that the machine is running :)"

Change a website to use a specific user in app pool (impersonate:false)

How to script moving a website username/password from web.config to an app pool is pretty easy if you have the correct syntax.

First, you need to use a tool called appcmd. that is located and must be ran from the following location:

c:\windows\system32\inetsrv

Before you make the change, this process updates the applicationHost.config file AND the web.config in the website directory so ALWAYS make a backup.

Simply copy the file called applicationHost.config from C:\Windows\System32\inetsrv\config so you have a backup.

Also copy the web.config from the website you’re changing as well.

Once your backups are done, open a CMD prompt and change directory to c:\windows\system32\inetsrv and run the following commands:

appcmd set config “SITE_NAME_HERE” /section:identity /impersonate:false

appcmd set config /section:applicationPools /[name=’SITE_NAME_HERE’].processModel.identityType:SpecificUser /[name=’SITE_NAME_HERE’].processModel.userName:DOMAIN\user_name /[name=’SITE_NAME_HERE’].processModel.password:Password_Here

it isn’t necessary to restart the app pool but it doesn’t hurt to do so.

To verify:

Test the site… Check the site logs… Check your security logs…

All done!

How To: Delete a NetApp Snapshot Relationship using OnCommand and CLI

If the schedule is in VMWare, go to the Netapp snapin in VMWare and delete the schedule

Go to the source filer in OnCommand and Quiesce the snapshot
To check that the snapshot is quiesced, log into the dest filer via the CLI and run this command:

Dest-Filer> snapmirror status -l

Source: Source_Filer:nonprod_vol17
Destination: Dest-Filer:nonprod_lun17_vol_Snapmirror
Status: Idle
Progress: –
State: Quiesced
Lag: 02:46:59
Mirror Timestamp: Tue Jul 31 07:03:11 CDT 2012
Base Snapshot: Dest-Filer(0135095600)_nonprod_lun17_vol_Snapmirror.525
Current Transfer Type: –
Current Transfer Error: –
Contents: Replica
Last Transfer Type: Update
Last Transfer Size: 4530884 KB
Last Transfer Duration: 00:00:59
Last Transfer From: Source_Filer:nonprod_vol17

the status will show Quiesce

on the source filer in OnCommand select Break

now run the same command on the dest. you will see that it’s Broken-off:

Dest-Filer> snapmirror status -l

Source: Source_Filer:nonprod_vol17
Destination: Dest-Filer:nonprod_lun17_vol_Snapmirror
Status: Idle
Progress: –
State: Broken-off
Lag: 02:51:34
Mirror Timestamp: Tue Jul 31 07:03:11 CDT 2012
Base Snapshot: Dest-Filer(0135095600)_nonprod_lun17_vol_Snapmirror.525
Current Transfer Type: –
Current Transfer Error: –
Contents: Replica
Last Transfer Type: Update
Last Transfer Size: 4530884 KB
Last Transfer Duration: 00:00:59
Last Transfer From: Source_Filer:nonprod_vol17

Now in OnCommand, the “Delete” button will be available. Delete the SnapMirror relationship and it will delete it on both sides.

you can also check if the snapmirror exists in the dest again. If it does, something went wrong 🙁

Pat yourself on the back!