PowerShell and Roku API

Below is a simple one liner to send a command to your Roku via PowerShell command.

Invoke-WebRequest -uri 'http://192.168.1.7:8060/keypress/home' -Method POST -Body ""


Pull AD Group Members and Member Properties

##Pull Active Directory Group Members, and Member Properties
##———————————————————–
##Get AD Groups via text file or Get-ADGroup command
$adGroups = Get-Content “C:\Temp\adGroups.txt”;
#$adGroups = Get-ADGroup -Filter *;

Foreach ( $group in $adGroups ) {
     $groupName = $group;
     #$groupName = $group.Name;
     $adGroupMembers = Get-ADGroupMember $groupName;

     Foreach ( $member in $adGroupMembers ) {
          $memberSamAcct = $member.SamAccountName;
          $adUser = Get-ADUser $memberName;
          ##Get other AD User attributes here
          echo “$groupName,$memberSamAcct”;
     }
}


PowerShell Compare User Info in Multiple Domains

Below is a script to compare a user’s Given Name between two domains.  Useful for a development and production comparison.

##Get Domain Credentials
$domain1Creds = Get-Credential ##Enter credentials for first domain;
$domain2Creds = Get-Credential ##Enter credentials for first domain;

##Get Domain Users
$adUsersD1 = Get-ADUser -Filter * -Server Domain1 -Credential $domain1Creds;
$adUsersD2 = Get-ADUser -Filter * -Server Domain2 -Credential $domain2Creds;

Foreach ( $user in $adUsersD1 ) {
     $userName = $user.SamAccountName;
     $userGivenName = $user.GivenName;

     ##Get Index and entry of user in the adUsersD2 array
     $d2Index = $adUsersD2.SamAccountname.IndexOf($userName);
     $user2Compare = $adUsersD2[$d2Index];

     ##Compare Given name from domain1 to domain2
     if ( $userGivenName -ne $user2Compare.GivenName ) {
          echo “$userName Given name is different in domain2”;
     }
}


Validate Web Site is Up – Powershell

##Create WebClient
$webClient = New-Object System.Net.WebClient;
try {
     ##Download site source code
     $source = $webClient.DownloadString(“webURL”);
} catch {
     ##Couldn’t connect
     echo “Down!”;
}


PowerShell and Regular Expressions

Here is a simple example of finding matches in a line of text using Regular Expressions.

In the example below I am searching for the word “test” in the variable named $myVar.

The command $myRegEx = [regex]”test” defines what we are looking for and sets the variable to a regex type.

The third command searches through $myVar for the word test and the next command shows count of findings.

Image


WebPart Targeting with Powershell

WebPart Targeting with Powershell in SharePoint 2013

$web = Get-SPWeb "http://sharepoint/Page2";
$sharepointGroups = ';;Group1,Group2';
$webPath = "pages/yourpage.aspx";
$webPM = $web.GetLimitedWebPartManager($webPath, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared);
$webParts = $webPM.WebParts;
$myWebPart = $webParts | where { $_.Title -match "Web Part name" };
$myWebPart.AuthorizationFilter = [string]"$sharepointGroups";
$webPM.SaveChanges($myWebPart);
$web.Dispose();

Rename Quick Launch Item

Rename Quick Launch Item with Powershell in SharePoint 2013

$web = Get-SPWeb "http://sharepoint/Page1";
$quickLaunchNav = $web.Navigation.QuickLaunch;
$item2Rename = $quickLaunchNav | where { $_.Title -match "itemName" };
$item2Rename.Title = "NewItemName";
$item2Rename.Update();
$web.Dispose();