Category: PowerShell

Powershell: Get Sharepoint 2010 Site Collection Storage Usage

If you need to get the Storage Usage from a Sharepoint 2010 Site Collection, you can do this with Powershell.

Current Storage Used

1. Start SharePoint 2010 Management Shell or Powershell with the Sharepoint Snapin by:

Add-PSSnapin "Microsoft.SharePoint.PowerShell"

2. Create a new Object for a Site Collection:

$MySite = Get-SPSite http://my.sharepoint.com

3. You can now check the members of the $MySite Object:

$MySite | Get-Member (You could also use: Get-SPSite http://mysite.sharepoint.com | Get-Member)

get-spsite http://mysite.sharepoint.com | get-member

4. Now you get an output with a lot of member properties of this Site Collection Object. And you can see there is a member property called Usage, I think thats the right property. Now lets see what we get here:

$MySite.Usage

retruns:

Storage           : 1775819
Bandwidth         : 0
Visits            : 0
Hits              : 0
DiscussionStorage : 0

Ah there is a Storage property, lets use this to only get the used Storage:

$MySite.Usage.Storage

this returns the Storage Space in Bytes. Not really use full, so we get this in Megabytes:

$MySite.Usage.Storage / 1MB

returns:

1.6935....



PowerShell 7 Null conditional operators

Powershell: check variable for null

If you are working a lot with PowerShell parameters and inputs you need to check if variables have the right value and are not “null”. Here is how you can check a PowerShell variable is null. In addition, I am also going to share how you can use the Null conditional operators in PowerShell 7.

PowerShell check variable for null

The normal if -eq “$null” doesn’t work:

if ($varibalename -eq $null) { Write-Host "variable is null" }

Now how you can check that (check if $variablename has $null as value):

if (!$variablename) { Write-Host "variable is null" }

And here if you wanna check if $variablename has any value except $null:

if ($variablename) { Write-Host "variable is NOT null" }

Null assignment and coalescing operators (?? and ??=) in PowerShell 7

If you are running the new PowerShell 7 version, you have new Null conditional operators to make this simpler.

PowerShell 7 Null conditional operators
PowerShell 7 Null conditional operators

With the new PowerShell ?? null coalescing operator to get the value of a statement if it’s not $null or return something else if it is $null. And with the new ??= null conditional assignment operator, you can easily assign a variable a value, but only if the variable is $null.

Here are some examples:

#Variable which is null

$myVariable

#Check variable for null

$myVariable ?? "Is null? True!"

#Check variable for null, and if it is null set the following value

$myVariable ?? "Set the value if null!"

If you want to learn how you can install and update PowerShell 7, check out my blog post.

Conclusion

I hope this helps you to check if your PowerShell variable is null or has a value assigned. If you have any questions, feel free to leave a comment.



Powershell: Send Tweet

Powershell Header

For some reason your Powershell script should send a Tweet. With this function Powershell will send a message to Twitter.

function Send-Tweet($Message)
{
#Twitter Login
$Twitter_UserName = "yourname"
$Twitter_Password = "yourpassword"
$Twitter_URL = "https://twitter.com/statuses/update.xml?status="

$Twitter_Request = [System.Net.WebRequest]::Create($Twitter_URL + $Message)
$Twitter_Request.credentials= New-Object System.Net.NetworkCredential($Twitter_UserName,$Twitter_Password)
$Twitter_Request.method= "POST"
$Twitter_Request.contentType = "application/x-www-form-urlencoded"
$Twitter_Request.GetResponse().statusCode # return the status code of the request
}

You can now call this function and add a message

Send-Tweet "This is my first Tweet from Powershell"

Twitter

If you need a interactive script you can simply use this (seen by Bhargav’s IT Playground)

function Send-Tweet($Message,$UserName){
if($Message-eq$null){$Message=Read-Host"Enter your tweet"}
if($Username-eq$null){$Username=read-host"Enter your twitter username"}
if($Password-eq$null)
{
$Password=read-host-assecurestring"Enter your twitter password"
$marshal=[Runtime.InteropServices.Marshal]
$Password=$marshal::PtrToStringAuto($marshal::SecureStringToBSTR($Password))
}
$url="https://twitter.com/statuses/update.xml?status=$Message"
$request=[System.Net.WebRequest]::Create($url)
$request.credentials=New-Object System.Net.NetworkCredential($UserName,$Password)
$request.method="POST"
$request.contentType ="application/x-www-form-urlencoded"
$request.GetResponse().statusCode # return the status code of the request}



PowerShell

Powershell: Parsing XML part 1

In my company, we started to use XML files as configuration files. So we use it for some configurations of servers or automation for our robots. In Powershell, there is a pretty easy way for parsing XML. In Powershell there is an object type for XML, so you just can use the get-content Cmdlet to read the XML file into the object.

$xmldata = get-content "C:\XMLFiles\mydata.xml"

When you create a function for reading XML files you also can set the XML object to global.

$global:xmldata = get-content "C:\XMLFiles\mydata.xml"

Now $xmldata is an object which includes all the data of the mydata.xml.

Let’s view the content of the mydata.xml file. We need this to understand the following commands.

<TodoList ID = "Week21"> 
<task action="create" ID="1"> 
<name>Peter</name> 
<dept>Administration</dept> 
<email>[email protected]</email> 
</task> 
<task action="create" ID="2"> 
<name>Thomas</name> 
<dept>Administration</dept> 
<email>[email protected]</email> 
</task> 
<task action="delete" ID="3"> 
<name>Steve</name> 
<dept>IT</dept> 
<email>[email protected]</email> 
</task>

Now we wanna get all unique Accounts listed

$xmldata.TodoList.Task | %{$_.Name} | select-object -unique
Peter
Thomas
Stefan

We can also create a new object for a specific Task

[Object]$xmltask2 = $xmldata.TodoList.Task | Where-Object {$_.ID -eq "2"}

Now we have a new object with data from Task ID 2. If we wanna see the name from this we just use the same command with the new object

$xmltask3 | {$_.Name}
Thomas

We can do a lot more with this, but I will bring this up in the next blog posts.



Powershell: Invoke-Expression

Powershell Header

Using the Invoke-Expression Cmdlet:

The Invoke-Expression cmdlet provides one way to run a script from within Windows PowerShell.

Invoke-Expression c:\scripts\test.ps1

or

& c:\scripts\test.ps1

And you also can run a function in your Powershell Script by a variable. This is something really important when you work with dynamic scripts:

Invoke-Expression -command ($Function_Name)

you even can send dynamic parameters to this function:

Invoke-Expression -command ($Function_Name + ' $Function_Parameters')