Archive | PowerShell

Quickly install the SQL powershell toolls on your local machine

I wanted to quickly install the 2012 powershell tools on to my machine. I could’t find a simple summary, so here goes:

Visit this site:
http://www.microsoft.com/en-us/download/details.aspx?id=29065

Download the following:

Microsoft® Windows PowerShell Extensions for Microsoft® SQL Server® 2012
Microsoft® SQL Server® 2012 Shared Management Objects
Microsoft® System CLR Types for Microsoft® SQL Server® 2012

Wherever you downloaded the above files to:

PS C:\Temp> .\SQLSysClrTypes.msi /qr /norestart
PS C:\Temp> .\SharedManagementObjects.msi /qr /norestart
PS C:\Temp> .\PowerShellTools.MSI /qr /norestart

Import-Module SQLPS -DisableNameChecking 

That should do it.

Quick PowerShell script to check DNS settings on all servers

I wanted to decommission some old Domain Controllers. I needed to make sure that other servers weren’t pointing to theses old DCs for DNS. I wrote this quick PowerShell script to loop through all servers and get their DNS search order.

$AllServers=Get-ADComputer -Filter {OperatingSystem -Like "Windows Server*"}
ForEach ($Server in $AllServers){
$Result=Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "IPEnabled = 'True'" -Property DNSServerSearchOrder -ComputerName $Server.Name 
$output = new-object PSObject 
$output | add-member NoteProperty "ComputerName" $Server.Name
$output | add-member NoteProperty "DNSServerSearchOrder" $Result.DNSServerSearchOrder
$output
}

Hope that helps some one, or me when we moce to the next version of DCs.

Quick PowerShell script to “tail -f” dhcp logs

When I am working in Linux, I like to leave a log open with “tail -f”, so I can see the results of some test I am performing.

The other day I wanted to see when a new machine joined the network, so I could give it a static DHCP lease. Usually I connect to the DHCP server find the DHCP logs and open them in notepad. I finally wrote a quick script to “tail -f dhcp.log” in PowerShell.

 

function JBM-AD-GetDHCPLogs {
 PARAM($ServerName="dhcpServerName")
 $FileName="DhcpSrvLog-$(get-date -format ddd).log"
 $PATH="\\$ServerName\c$\Windows\System32\dhcp\$FileName"
 Get-Content $path –Wait
 }

Import Picture into AD with PowerShell

I know this is everywhere, so this is more a note for myself. How to upload pictures to AD via PowerShell:

Import-RecipientDataProperty -Identity username -Picture -FileData ([Byte[]]$(Get-Content -Path .\username.jpeg -Encoding Byte -ReadCount 0))

PowerShell 3: Using Invoke-RestMethod to refresh a new oAuth 2 token

I wanted to translate this code into powershell. Below is the Powershell code to request a refresh token from Google using oAtuh 2.

$CLEINTID="1234567890.apps.googleusercontent.com"
$CLIENTSECRET="aBcDeFgHiJkLmNoPqRsTuVwXyZ"
$REFRESHTOKEN="1/551G1yXUqgkDGnkfFk6ZbjMLMDIMxo3JFc8lY8CAR-Q"

$URL = "https://accounts.google.com/o/oauth2/token"
$Body= 'client_secret={0}&grant_type=refresh_token&refresh_token={1}&client_id={2}' -f $CLIENTSECRET,$REFRESH_TOKEN,$CLEINTID
Invoke-RestMethod -URI $URL -Method Post -Body $Body

Hope that helps someone.

My upgrade of SharePoint 2007 to 2010 “script”

One of my most recent projects was the migration of our intranet from SharePoint 2007 to 2010. Since we were going to change the name of the site, I was able to run through this “script” several times as practice to make sure I had everything correct.

I decided to do a detach and attach method. Here are some of the things we did.

  1. We preformed several test a detach and attach upgrades with the new URL. This allowed us to test everything using the new url, and make changes back in the original 2007 site so that it would work once we performed the final live cutover.
  2. All new code/pages/hacks were added to the 2010 site into New documents libraries. These were backed up using this script and restored after every new test detach an attach test. This way all new code would be in place with the final live cutover.
  3. Since we were doing a new Navigation, we created the navigation in the old 2007 site, and hid them by audience. Then one of the steps below is to change the audience which would un-hide the new navigation in the final cutover.

Step 1. Backup all the new code/pages/hacks that have been added to the new site that needs to be restored.

$folderDate=$(get-date -uformat "%Y-%m-%d")
$folderHour=$(get-date -uformat "%H")
$backupDir="\\Path\To\Backup\$folderDate\$folderHour"

foreach ($web in $(Get-SPSite | Get-SPWeb)){
    foreach ($list in $web.Lists) {
    mkdir -force "$backupDir\$($Web.Title.replace(' ','').replace('/','-'))\"
    Export-SPWeb $($web.Url) -itemurl "$($list.RootFolder.ServerRelativeUrl)" -path "$backupDir\$($Web.Title.replace(' ','').replace('/','-'))\$($list.Title.replace(' ','').replace('/','-')).cmp"
    }
}

Now we have captured all the changes that were made to the new site (which we will be restoring after the next cutover test)

Step 2. Remove the previous test cutover site

Remove-SPWebApplication "SharePoint - intranet.company.com80" -RemoveContentDatabases -DeleteIISSite

Step 3. Re-create new app and apppool.

New-SPWebApplication -Name "SharePoint - intranet.company.com80" -Port 80 -HostHeader intranet.company.com -URL "http://intranet.company.com" -ApplicationPool "SharePoint - intranet.company.com80"

Step 4. remove the content database that is created by default

Get-SPContentDatabase -WebApplication "SharePoint - intranet.company.com80" | Remove-SPContentDatabase

Step 5. Backup 2007 site to a share and restore to new SQL server (or new db name on existing SQL server).

Backup:

$SRCSQLSERVER='OldSQLServer'
$DESTSQLSERVER='NewSQLServer'
$sqlcmdBackup="BACKUP DATABASE [Content_Intranet] TO DISK = N'\\path\to\network\share\Content_Intranet.bak' WITH NOFORMAT, NOINIT,  NAME = N'Content_Intranet FullBackup', SKIP, NOREWIND, NOUNLOAD,  STATS = 10"
invoke-sqlcmd -query "$sqlcmdBackup" -Server $SRCSQLSERVER -QueryTimeout 1200

Restore:

$sqlcmdRestore="RESTORE DATABASE [Content_Intranet] FROM  DISK = N'\\path\to\network\share\Content_Intranet.bak' WITH  FILE = 1,  MOVE N'Content_Intranet' TO N'K:\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\Content_Intranet.mdf',  MOVE N'Content_Intranet_log' TO N'K:\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\Content_Intranet_log.LDF',  NOUNLOAD,  REPLACE,  STATS = 10"
invoke-sqlcmd -query "$sqlcmdRestore" -Server $DESTSQLSERVER -QueryTimeout 1200

Step 6. Mount the restored database and upgrade the uner experience.

Mount-SPContentDatabase -Name Content_Intranet  -DatabaseServer NewSQLServer -WebApplication http://intranet.company.com -Updateuserexperience

Step 7. Re-import exported Document Libraries that contain the new code/pages/apps

Import-SPWeb http://intranet.company.com -Path \\path\to\network\share\date\hour\LibraryName.cmp

Step 8. Clean up navigation by changing audience, change homepage to new page in restored Document Libraries.

Step 9. Alter web.config for Cisco WebVPN

Step 9. Allow inline PDFs

That was it. I did it several times, and it ended up being a smooth cutover.

PowerShell command to allow inline PDF viewing in SharePoint 2010

My users like to view PDFs in their browser on our SharePoint site. I needed to allow this in 2010:
Here is the powershell to allow inline PDF viewing in SharePoint 2010

$webapps = Get-SPWebApplication "SharePoint - intranet.company.com80"
foreach ($webapp in $webapps) 
{ 
    $webapp.AllowedInlineDownloadedMimeTypes.Add("application/pdf") 
    $webapp.Update() 
}

PowerShell 3: Invoke-WebRequest vs Invoke-RestMethod and a SharePoint 2010 list with more than 1000 entries

When using Invoke-RestMethod with a SharePoint 2010 list and ListData.svc, it returns an “System.Xml.XmlElement#http://www.w3.org/2005/Atom#entry” object. Not sure why, but the end result is that you can’t get access to the “rel=next” url, or am I doing something wrong?

$Results=Invoke-RestMethod -uri $ListUrl -UseDefaultCredentials
$Results | gm 
TypeName: System.Xml.XmlElement#http://www.w3.org/2005/Atom#entry

I had to use Invoke-WebRequest and then take the Content and put it in an XML variable, only then could I get to the next page of items.

$MailingLabels = Invoke-WebRequest -Uri $ListUrl -UseDefaultCredentials
$Next =  ($MailingLabelsXML.feed.link | ?{$_.rel -eq "next"}).href

Thoughts?

Powered by WordPress. Designed by WooThemes