Microsoft – SharePoint – List of uploaded documents

 

Background

This simple little code has been bugging me for a while.

I have a set of web pages that contains documents and I need to programmatically get the list of images available via hyperlink.

 

Code

Thanks to our web friends, here is as far as we got:

$url = "https://www.myimages.org"

$r = Invoke-WebRequest -URI $url -UseDefaultCredentials

$r.Links |
	  where {$_.href -like '*jpg*'} |
		select outertext

 

Listening

I will fess up and say I sampled Good Kid Rapper, … don’t kill my vibe.  But, it is not kid friendly. And, so went YouTube fishing and searched for “Christian Music 2014” and found a new group:

Switchfoot — Love Alone Is Worth The Fight
https://www.youtube.com/watch?v=rk9Pj3ID0UE&index=20&list=PLWvahZRxLnLMSWeULnub0CCeQzvxGM8hB

References

 

 

 

References – Powershell – Invoke-WebRequest

 

References – Powershell – Operators

Powershell – Writing out Array Elements

 

Background

As I went back and started closing out the many Google Chrome Windows and Tabs that I had opened, I took another look at a blog post titled “A Taste of PowerShell – String Manipulation: Splitting and Joining Strings” –http://tasteofpowershell.blogspot.com/2009/08/string-manipulation-splitting-and.html .

 

Introduction

The Blog post has a very concise code that was quite honestly over my head.  It accesses a data file and read and emit the contents.

Actual Code:


Get-Content people.txt |
>> %{$data = $_.split("`t"); Write-Output "$($data[1]) $($data[0]), $($data[2])"}

 

I tried changing it to


Get-Content people.txt | %{$data = $_.split("`t"); Write-Output "$($data[1]) $($data[0]), $($data[2])"}

 

so that I can see everything on one line, but I am still thinking to myself what does the % sign mean.

Later I found that it is a shortcut for iterating through a list. i.e. for / while.

 

Sample Rewrite attempts

Here is my attempts at rewrites.

 

($data[2]):

Here is what happens when we have ($data[2]) as part of Write-Output

Get-Content people.txt | %{ $data = $_.split("\s+"),3; Write-Output "First Name: ($data[2]) " }

Output:

output1

($arrayPeople[0]):

Here is what happens when we have ($arrayPeople[0]) as part of Write-Output


(Get-Content people.txt ) | %{ $data = $_; $arrayPeople = $data -split "\s+"; Write-Host "First Name: $arrayPeople[0]"; }

Output:

output2

($arrayPeople[0]) ($arrayPeople[1]) (arrayPeople[2]):

Here is what happens when we have ($arrayPeople[0])  ($arrayPeople[1]) ($arrayPeople[2]) as part of Write-Output


(Get-Content peoplepiped.txt ) | %{ $data = $_; $arrayPeople = $data -split '\s+';  Write-Host  "($arrayPeople[0]) ($arrayPeople[1]) in ($arrayPeople[2])" ; }

Output:

output3

 

 

Using format -f argument1, argument2, argument3 ( Works ):

Using format ( -f ) argument1, argument2, argument3

(Get-Content people.txt ) | %{ $data = $_; $arrayPeople = $data -split '\s+'; "{0} {1} in {2} " -f $arrayPeople[1], $arrayPeople[0] , $arrayPeople[2] }

Output:

output4

 

 

Write-Host ( Works ) :

Preceding the array element with a dollar using ($) — $(arrayVar[position])


(Get-Content people.txt ) | `
	%{ 
		$data = $_; 
		$arrayPeople = $data -split '\s+'; 
		Write-Host "$($arrayPeople[0]) $($arrayPeople[1]) in $($arrayPeople[2])" ; 
	 }


Output:

output4

 

 

Summary

When array elements are referenced within a Write-Host statement, please surround with dollar sign ($)….$($Array[array-element]).

Or use composite formatting.

Syntax:


"{} {1} in {2} " -f variable-1, variable-2 , variable-3

 

Sample:


"{0} {1} in {2} " -f $arPeople[1], $arPeople[0] , $arPeople[2]

 

Parting Comment

Sorry it took a bit of time to explain what one hopes will be easy to code; the idea of printing array elements.

Now when I go back and look at the earlier code, I see the importance of the $sign that precedes each array element:


    Get-Content people.txt | %{$data = $_.split("`t"); Write-Output "$($data[1]) $($data[0]), $($data[2])"}

 

Reference

Powershell – Q/A

Powershell – Format

 

Powershell – Composite Formatting

 

Powershell – String Manipulation

 

Powershell – Get-Content

 

Technical : Microsoft – Event Log / Event Viewer – Query via PowerShell

Technical : Microsoft – Event Log / Event Viewer – Query via PowerShell

Introduction

After my fiasco of not paying attention to my Event Log, I need a quick way to aggregate Event Log entries and here is what I came up with through curating Google web postings.

Code



# PowerShell example which groups event then sorts in descending order.
Clear-Host

$logName = "System"

#get date
$dateCurrent = Get-Date

#For the Week
$NDays = -7
$dateNDaysAgo = $dateCurrent.AddDays($NDays)

Write-Host "Log Entries since $dateNDaysAgo"

Get-WinEvent -Logname $logName  | `
where-object { $_.timecreated -gt $date  -and $_.levelDisplayName -in "Critical", "error", "warning" } | `
Group-Object ProviderName, levelDisplayName, ID | `
Sort-Object Count -descending | `
Format-Table Count, Name -auto

Output:

EventLogAggregator

Conclusion

And, yes I have too much time on my hands, and not enough brains; as I need Splunk, Nagios, or System Center.

Addendum

Addendum – 2014.01.20

Tried the code above on an MS Windows Vista Machine, but unfortunately it failed.

The original code was developed and targeted an MS Windows 2012 box.

The error message we got on the Vista box read:


You must provide a value expression on the right-hand side of the '-' operator.
At C:\temp\eventvwr.ps1:16 char:68
+ where-object { $_.timecreated -gt $date  -and $_.levelDisplayName - <<<< in "
Critical", "error", "warning" } | `
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : ExpectedValueExpression

To Correct:



# PowerShell example which groups event then sorts in descending order.
Clear-Host

$logName = "System"

#get date
$dateCurrent = Get-Date

#For the Week
$NDays = -7
$dateNDaysAgo = $dateCurrent.AddDays($NDays)

$logLevel = @("Critical", "Warning", "Error")
$logLevelCritical = "Critical"
$logLevelWarning = "Warning"
$logLevelError = "Error"

Write-Host "Log Entries since $dateNDaysAgo"

Get-WinEvent -Logname $logName  | `
where-object {   ($_.timecreated -gt $date)  `
                 -and ( `
                       ($_.levelDisplayName -eq $logLevelCritical)  `
                   -or ($_.levelDisplayName -eq $logLevelWarning)   `
                   -or ($_.levelDisplayName -eq $logLevelError)     `
                      )  } | `
Group-Object ProviderName, levelDisplayName, ID | `
Sort-Object Count -descending | `
Format-Table Count, Name -auto

References

Event Log

WMI

Technical – PowerShell / Microsoft .Net CLR Version

Technical – PowerShell / Microsoft .Net CLR Version

 

Background

A week or so ago, I tried running Microsoft SharePoint 2010 Administration Toolkit on an MS Windows box.

I ran into problems due to the SharePoint diagnostic tool not being compatible with the default PowerShell/.Net Framework combo on the Windows 2012 box.

Here is the first in a series of post on the school yard beating I took.

Introduction

In this post, I will limit our discussion to ensuring that our pre-requisites are installed and functioning.   Those pre-requisites are Microsoft PowerShell and Microsoft .Net v4. and v3.5.

Confirm installation of Microsoft .Net 3.5 & PowerShell 2.0 Engine

Courtesy of (http://technet.microsoft.com/en-us/library/hh849675.aspx), here are the install\confirmation steps:

Add Microsoft .Net 3.5

  • In Server Manager, from the manage menu, select the Add Roles and Features
  • Select the Server and click on the “Add Roles and Features” option
  • On the Installation Type page, select “Role-based or feature-based” installation
  • On the features page, expand the .Net 3.5 Framework features and select .Net Framework 3.5 (includes .Net 2.0 and 3.0)

WindowsDotNetFrameworkInstallation

To add the Windows PowerShell 2.0 Engine feature

  • In Server Manager, from the manage menu, select the Add Roles and Features
  • Select the Server and click on the “Add Roles and Features” option
  • On the Installation Type page, select “Role-based or feature-based” installation
  • On the features page, expand the “PowerShell (installed)” option and select the “Windows PowerShell 2.0 Engine” option

WindowsPowerShellInstallation

Get OS/CLR Version

To get OS/CLR Version within PowerShell, we can try the following commands:

  • [System.Environment]::OSVersion.Version
  • (Get-WmiObject -class Win32_OperatingSystem).Caption
  • $PSVersionTable

The most important for our current discussion is the $PSVersionTable command:


$PSVersionTable

Output:

On MS Windows 2008/R2 box, you will see:

versionInfoOS2008R2Standard

On MS Windows 2012, you will see:

window2012-default

On MS Windows 2012, if your application demands and requires Version 2, then based on the example documented below, add “-version 2” as a command line argument when starting Poweshell

Syntax Screen:

PowerShellCommandLineVariable

So we issue :


powershell -version 2

And, replay the Version and CLR Info we previously mentioned:

PowershellCLR2onMSWindows2012

Summary:

Here is a quick run-down of OS, PowerShell Version’s support along with Microsoft.Net CLR support and thereof backward compatibility.

OS PSVersion CLRVersion CLR Version Backward
Win2008-R2  2.0 2.0.50727.572 {1.0,2.0}
Win2012  3.0 4.0.30319.18051 {1.0,2.0,3.0}
Win2012 (-v2)  2.0 2.0.50727.6407 {1.0, 2.0}

System Info Tools

As said, PowerShell loads Microsoft.Net dll depending on the OS Version and/or command line parameters.

One popular tool for reviewing system information and activities are of-course SysInternals set of tools.

Windows 2012

In the screen-shot above, we initiated PowerShell on an MS Windows 2012, and did not pass in any command-line parameter:

SysInternals-PowerShell-Windows2012-Image

Here are the Microsoft .Net Dlls loaded:

SysInternals-PowerShell-Windows2012-DotNetAssemblies

From the screen-shot above, we can see the following:

  • on the left panel, we can see that we are on CLR v4.0
  • On the right panel, we have the listing on .Net 4.0 native assemblies loaded

Windows 2012 (passed in -version 2.0)

The screen below indicates what things look like when we pass in the “-version 2.0” option; please pay attention to the “command line” section.

SysInternals-PowerShell-Windows2012-with-v2-Image

And, here is CLR Version & DLL Listings

SysInternals-PowerShell-Windows2012-with-v2-DotNetAssemblies

So we are able to confirm that when we pass in the “-version 2.0” option, we get PowerShell to go back in time.

Conclusion

In follow-up posts, I will discuss the actual problems I ran into and the series of fruitless steps I tried to correct it.

References

References – Determine Version#

References – PowerShell 2.0 Engine