tag:blogger.com,1999:blog-86836932024-03-14T03:25:01.438-05:00Kevin Marquette - The things I do.Some problems you just can't search on. Here are some I wish were more searchable and this blog is my attempt to make that happen.Kevin Marquettehttp://www.blogger.com/profile/16389030891301082770noreply@blogger.comBlogger149125tag:blogger.com,1999:blog-8683693.post-80529015077614032472016-10-15T16:38:00.003-05:002016-10-16T22:26:58.758-05:00Taking a break from BloggerHello everyone. Thank you for visiting my blog. My first post in blogger on this account was 12 years ago. I think it is time to try something new. I am transitioning over to a new blog at <a href="https://kevinmarquette.github.io/">https://kevinmarquette.github.io/</a><br />
<br />
This new blog will be based on markdown and sourced out of my github account. I am trying to connect more with community projects and I find myself working a lot out for github. I also post a lot of content at <a href="http://www.reddit.com/r/powershell">http://www.reddit.com/r/powershell</a> that should translate easier into content onto the new blog.<br />
<br />
Only time will tell how long I stick with thew new blog. I am leaving this on live because I have a lot of content that I don't think will export very well. I say that because I use lots of script block and have changed my approach several times over the years. The source on some of them look good online but are very nasty to parse.Kevin Marquettehttp://www.blogger.com/profile/16389030891301082770noreply@blogger.com0tag:blogger.com,1999:blog-8683693.post-89885536442059640892016-03-21T23:12:00.000-05:002016-03-21T23:12:06.555-05:00Powershell: Using optional paramatersHave you ever created a cmdlet that was a wrapper for another command but you added optional parameters. This is a very common pattern. But how do you pass or not pass that optional parameter to the underlying command? How often do you create an if or switch to check for it and have the function call in your code twice? Once with the param and once with out it.<div>
<br /></div>
<div>
This is such a common pattern especially when dealing with credentials. I saw this <a href="https://www.reddit.com/r/PowerShell/comments/4aqujo/help_pipelining_incoming_string_objects_to/" target="_blank">example </a>in someone else's code:</div>
<div>
<br /></div>
<div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkblue; font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;">if</span><span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;">(<span style="color: orangered;">$Credential</span>)
<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: darkblue;">foreach</span>(<span style="color: orangered;">$Computer</span> <span style="color: darkblue;">in</span>
<span style="color: orangered;">$ComputerName</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: orangered;">$Disks</span>
<span style="color: darkgrey;">+=</span> <span style="color: blue;">Get-WmiObject</span>
<span style="color: blueviolet;">Win32_LogicalDisk</span> <span style="color: navy;">-ComputerName</span> <span style="color: orangered;">$Computer</span> <span style="color: navy;">-Filter</span> <span style="color: darkred;">"DeviceID='C:'"</span>
<span style="color: navy;">-Credential</span> <span style="color: orangered;">$Credential</span>
<span style="color: navy;">-ErrorAction</span> <span style="color: blueviolet;">SilentlyContinue</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;">}
<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkblue; font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;">else</span><span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: darkblue;">foreach</span>(<span style="color: orangered;">$Computer</span> <span style="color: darkblue;">in</span>
<span style="color: orangered;">$ComputerName</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: orangered;">$Disks</span>
<span style="color: darkgrey;">+=</span> <span style="color: blue;">Get-WmiObject</span>
<span style="color: blueviolet;">Win32_LogicalDisk</span> <span style="color: navy;">-ComputerName</span> <span style="color: orangered;">$Computer</span> <span style="color: navy;">-Filter</span> <span style="color: darkred;">"DeviceID='C:'"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;">}
<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
</div>
<div>
The credential was optional to his use of the cmdlet. I just don't like to see code repeated like that. Sure, he could have placed the if logic inside the foreach loop. But he still has logic in two places. I see the error action is set to SilentlyContinue for one of them. I can't tell if that is intentional or it should have been on both.</div>
<div>
<br /></div>
<div>
This is that same code but using a hashtable and splatting to deal with the optional parameter.</div>
<div>
<br /></div>
<div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: orangered; font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;">$LogicalDiskArgs</span><span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: darkgrey;">=</span> @{<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;"> Class
<span style="color: darkgrey;">=</span> <span style="color: darkred;">'Win32_LogicalDisk'</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;"> Filter <span style="color: darkgrey;">=</span>
<span style="color: darkred;">"DeviceID='C:'"</span></span><span style="font-family: Inconsolata; font-size: 9pt;"> </span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkblue; font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;">if</span><span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;">(<span style="color: orangered;">$Credential</span>)
<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: orangered;">$LogicalDiskArgs</span><span style="color: darkgrey;">.</span>Credential
<span style="color: darkgrey;">=</span> <span style="color: orangered;">$Credential</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: orangered;">$LogicalDiskArgs</span><span style="color: darkgrey;">.</span>ErrorAction <span style="color: darkgrey;">=</span>
<span style="color: darkred;">'SilentlyContinue'</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;">}
<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkblue; font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;">foreach</span><span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;">(<span style="color: orangered;">$Computer</span>
<span style="color: darkblue;">in</span> <span style="color: orangered;">$ComputerName</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: orangered;">$Disks</span>
<span style="color: darkgrey;">+=</span> <span style="color: blue;">Get-WmiObject</span>
<span style="color: orangered;">@LogicalDiskArgs</span> <span style="color: navy;">-ComputerName</span>
<span style="color: orangered;">$Computer</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;">}
<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
</div>
<div>
The use of splatting makes this really easy to do. I like how this really shows the intent of using that error action with the optional parameter. </div>
<div>
<br /></div>
<div>
</div>
Kevin Marquettehttp://www.blogger.com/profile/16389030891301082770noreply@blogger.com1tag:blogger.com,1999:blog-8683693.post-69402300044182506602016-03-16T22:57:00.000-05:002016-03-16T22:57:04.826-05:00Showing the intent of your code in PowershellI hang out on <a href="https://www.reddit.com/r/PowerShell" target="_blank">/r/Powershell</a> quite often and I find myself talking about writing code that shows your intent quite often. When I initially started working with Powershell I was kind of put off by the verbosity of it. Over time I came to accept it and things like tab complete make it easy to work with.<br />
<br />
I turned that corner a long time ago and really like the verbosity of it now. When you combine that with clean code best practices that create self documenting code, Powershell becomes so easy to read and work with. I like it when my code makes it very easy to see what the intent of it is. I share a lot of code so this is something I find to be important in my work.<br />
<br />
My simple <a href="https://www.reddit.com/r/PowerShell/comments/4aonjg/selecting_array_items_1_to_last/" target="_blank">example</a> is trying to split a string and skipping the first item in the list. This is the code sample that was getting passed around:<br />
<br />
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: orangered; font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;">$obj</span><span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: darkgrey;">=</span> (<span style="color: blue;">Get-ADGroup</span> <span style="color: darkred;">'GroupName'</span>)<span style="color: darkgrey;">.</span>DistinguishedName<span style="color: darkgrey;">.</span>split(<span style="color: darkred;">','</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: orangered; font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;">$obj</span><span style="color: darkgrey; font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;">[</span><span style="color: purple; font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;">1</span><span style="color: darkgrey; font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;">..</span><span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;">$(<span style="color: orangered;">$obj</span><span style="color: darkgrey;">.</span>count <span style="color: darkgrey;">-</span> <span style="color: purple;">1</span>)<span style="color: darkgrey;">] <o:p></o:p></span></span></div>
<br />
<div class="MsoNormal">
<br /></div>
It worked and accomplished the goal. But compare it to this and think about what one shows the intent of the code better.<br />
<br />
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;">(<span style="color: blue;">Get-ADGroup</span> <span style="color: darkred;">'GroupName'</span>)<span style="color: darkgrey;">.</span>DistinguishedName <span style="color: darkgrey;">-split</span>
<span style="color: darkred;">','</span> <span style="color: darkgrey;">|</span> <span style="color: blue;">Select-Object</span>
<span style="color: navy;">-Skip</span> <span style="color: purple;">1 <o:p></o:p></span></span></div>
<br />
<div class="MsoNormal">
<br /></div>
In my code, I would take it one more step.<br />
<br />
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: orangered; font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;">$ADGroup</span><span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: darkgrey;">=</span> <span style="color: blue;">Get-ADGroup</span> <span style="color: darkred;">'GroupName'</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: orangered; font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;">$ADGroup</span><span style="color: darkgrey; font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;">.</span><span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;">DistinguishedName
<span style="color: darkgrey;">-split</span> <span style="color: darkred;">','</span>
<span style="color: darkgrey;">|</span> <span style="color: blue;">Select-Object</span> <span style="color: navy;">-Skip</span> <span style="color: purple;">1</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<br /></div>
<br />
<div class="MsoNormal">
<o:p> All three of them are valid Powershell and this just a style preference, but having a solid and consistent can make your code a lot easier to read. </o:p></div>
<br />
<br />
<br />
<br />Kevin Marquettehttp://www.blogger.com/profile/16389030891301082770noreply@blogger.com0tag:blogger.com,1999:blog-8683693.post-4668033029898177642016-01-26T18:45:00.000-06:002016-01-26T18:45:05.746-06:00VM IO stalls on HP DL360 G7 ESXi 5.5.0 2403361 with a HP Smart Array p410 and a spare drive<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<br />
<div class="MsoNormal">
We have a product that is very sensitive to network issues.
So much so that it logs an error whenever it goes 5 seconds without
communication. Just so happened that we started logging this error after we
moved that product to a HP DL360 G7 running on ESXi 5.5. We first went down the
development route to track down the issue because we were doing load testing on
that product at the time.</div>
<div class="MsoNormal">
<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
At some point, we noticed these errors even when the system
was not under load. We could see these perfectly timed latency spikes at the
same time as we had those errors. Then we shifted our focus to
system/infrastructure. <o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
This is what our datastore latency looked like:<o:p></o:p></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://4.bp.blogspot.com/-TBMosjP0rDo/Vqfok30yuZI/AAAAAAAAAmM/JW-8BIlS39c/s1600/latency.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="175" src="http://4.bp.blogspot.com/-TBMosjP0rDo/Vqfok30yuZI/AAAAAAAAAmM/JW-8BIlS39c/s320/latency.png" width="320" /></a></div>
<div class="MsoNormal">
</div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<br /><br />
<div class="MsoNormal">
Then we discovered the strangest thing. We had this latency
even when no VMs were running. We configured a second system just like the
first to rule out the hardware. Sure enough, both systems had the problem.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
I started juggling drivers. I was quick to load new drivers
and firmware so that felt like a good place to start. I slowly walked the
storage controller driver backwards until the issue went away. I settled on
this driver:<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-family: Consolas;"><a href="http://h20564.www2.hpe.com/hpsc/swd/public/detail?swItemId=MTX_d18033ac346f468c92062ce127">http://h20564.www2.hpe.com/hpsc/swd/public/detail?swItemId=MTX_d18033ac346f468c92062ce127</a><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: Consolas;">Type: Driver - Storage Controller<br />
Version: 5.5.0.60-1(11 Jun 2014)<br />
Operating System(s): VMware vSphere 5.5<br />
File name: scsi-hpsa-5.5.0.60-1OEM.550.0.0.1331820.x86_64.vib (65 KB)<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-family: Consolas;"> <b>esxcli software
vib install -v file:/tmp/scsi-hpsa-5.5.0.60-1OEM.550.0.0.1331820.x86_64.vib --force
--no-sig-check --maintenance-mode<o:p></o:p></b></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
I didn’t like this as a solution because I had a new storage
controller firmware with a very old storage controller driver. I found another
DL360 G7 that was configured by someone else for another project and it was
running good. They didn’t update anything. Just loaded the ESXi DVD and ran
with it.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
I came to the conclusion that it had a valid firmware/driver
combination. I like that setup better than what I had so that became the
recommended deployment. Don’t update the firmware and use the DVD so you don’t
have to mess with drivers.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Then our next deployment that used that configuration and
the issue resurfaced. After ruling out some other variables that could have
impacted the results, I ended up rolling the driver back to fix it.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<o:p><a href="http://2.bp.blogspot.com/-qxu0AQ12y7I/VqfoxpHC-YI/AAAAAAAAAmU/jdlVP5WTmFM/s1600/latency2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" height="172" src="http://2.bp.blogspot.com/-qxu0AQ12y7I/VqfoxpHC-YI/AAAAAAAAAmU/jdlVP5WTmFM/s320/latency2.png" width="320" /></a></o:p></div>
<div>
<br /><div class="MsoNormal">
After pinning the driver down, I went hunting for more
information on it. Searching for latency issue with storage gives you all kinds
of useless results. I was hopeful that searching for the driver would lead me
to some interesting discussions. <o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<a href="https://communities.vmware.com/thread/472795?start=30&tstart=0">https://communities.vmware.com/thread/472795?start=30&tstart=0</a><o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<a href="http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=2075978">http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=2075978</a><o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<a href="https://communities.vmware.com/thread/492822?start=0&tstart=0">https://communities.vmware.com/thread/492822?start=0&tstart=0</a><o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<a href="https://communities.vmware.com/thread/526056">https://communities.vmware.com/thread/526056</a><o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
I ended up with an answer to my issue in that last link. But
there were a lot of other issues <o:p></o:p></div>
<div class="MsoNormal">
In summary, HPSA driver later than v60 driving a HP Smart
Array p410 controller of any firmware vintage with a spare drive configured.
The spare drive configuration seems to cause an i/o back-feed into the driver
blowing it up at irregular intervals. This issue will not surface unless a
spare drive is configured on the array controller.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
At the time of writing this, the v114 driver also does not
fix the issue.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<br />
<div class="MsoNormal">
<br /></div>
</div>
Kevin Marquettehttp://www.blogger.com/profile/16389030891301082770noreply@blogger.com0tag:blogger.com,1999:blog-8683693.post-59959143468318999712016-01-22T21:59:00.000-06:002016-01-22T21:59:55.999-06:00Powershell: Write-Host vs Write-Output in Functions?<div class="MsoNormal">
This comes up every so often. This is the question I saw
recently:<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<i>I like to have my scripts and functions display a minimal
level of progress on-screen by default, with more detailed progress listed by
using -verbose. Write-Host works fine for the minimal progress display, but I
keep reading horror stories of how the sky will fall if you use Write-Host
instead of Write-Output in Powershell. However ... Write-Output seems to
pollute the pipeline when trying to use the results of a function in a
pipeline.<o:p></o:p></i></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<i>Is there a best practice for this? Should you use
Write-Verbose for everything, or is Write-Host ok sometimes, or is there some
other common process? - /u/dnisthmnace</i><o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
One could argue that he has a solid understanding of it and
you is on the right track. You can write a lot of code for yourself this way
and everything would be OK.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
The higher level discussion is that use of Write-Host is
often a mental crutch that will slow your advancement of your Powershell
skills. The problem is not so much that you are using it but why you think you
need to be using it. But don't get hung up on that right now.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
My advice is to start breaking your scripts into smaller
pieces. Think of them as either tools or controller scripts. Tools are generic
and reusable. The tools should be single purpose (do one thing and do that
thing well). In your tools, use Write-Verbose.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Your controller scripts glue together your tools. Its OK for
them to have a little more polish for the end user. This is where write-host
would belong if you are using it (Purists can still use Write-Output). But the
thing is that this a end of the line script. This is not a script that should
be consumed by other scripts.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Personally, I use write-verbose for everything that I don't
want on the pipe (even in my controller scripts). I do this so I can choose
when I see the output and when I don't. When you start reusing code, this
becomes more and more important. Before you know it, you have functions calling
functions calling functions or functions executing with hundreds of data
points. I can even set verbose on a function call but let that function
suppress noisy sub functions.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
I use Write-Verbose in my controller scripts because they
can easily turn into tools and I don't have to go back and change the output.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
I hope that sheds some light on the discussion. It's not the
end of the world to use Write-Host, just know that you don't need to use it.
Once you understand that, you will start writing better Powershell.<o:p></o:p></div>
<br />
<div class="MsoNormal">
<br /></div>
Kevin Marquettehttp://www.blogger.com/profile/16389030891301082770noreply@blogger.com0tag:blogger.com,1999:blog-8683693.post-88912587917753059692016-01-21T19:53:00.000-06:002016-01-21T19:53:36.085-06:00Setting default parameters for cmdlets without changing them with $PSDefaultParameterValues<div class="MsoNormal">
I ran across something again recently that I find to be really
cool. You can add a command to your profile to add default parameters to any
cmdlet. I first saw this as a way to have Format-Table execute with the –AutoSize
flag every time.</div>
<div class="MsoNormal">
<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: orangered; font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;">$PSDefaultParameterValues</span><span style="color: darkgrey; font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;">.</span><span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;">Add(<span style="color: darkred;">'format-table:autosize'</span><span style="color: darkgrey;">,</span><span style="color: orangered;">$True</span>) <o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
That is a cool idea if you are stuck on Powershell 4 or
older. Now that Powershell 5 kind of does that already, I really didn’t think
much about it. Sometime later, I found myself wanting to share a cmdlet with
someone and the default parameter values were specific to the way I used it. I
didn’t really like that so I changed it. I made it into a better tool.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Except now I was supplying those values over and over every
time I used it for myself. I decided there had to be a better way and that’s when
I finally remembered this trick. And it can work on any cmdlet.<o:p></o:p></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: orangered; font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;">$PSDefaultParameterValues</span><span style="color: darkgrey; font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;">.</span><span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;">Add(<span style="color: darkred;">'Unlock-ADAccount:Server'</span><span style="color: darkgrey;">,</span><span style="color: darkred;">'domain.com'</span>) <o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
So now I can keep my cmdlets more generic and still get the benefit
of default parameters that benefit myself. I can stick in common server names or credentials. Any value that I can think of really. Just add it to my profile and I am all set.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<br />
<div class="MsoNormal">
<br /></div>
Kevin Marquettehttp://www.blogger.com/profile/16389030891301082770noreply@blogger.com2tag:blogger.com,1999:blog-8683693.post-8388811828943480082016-01-20T18:30:00.000-06:002016-01-20T18:30:01.494-06:00Powershell Error: Cannot process argument because the value of argument "value" is not valid. Change the value of the "value" argument and run the operation again.<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
This Powershell error <b>Cannot process argument because the value of argument "value" is not valid. Change the value of the "value" argument and run the operation again.</b> has tripped me up a few times now. I
can usually fix it without ever really knowing how. I hate those type of
solutions so this time I decided to get to the bottom of it.</div>
<div class="MsoNormal">
<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
I started using a new template snippet for my advanced
functions. I tracked it down to one small detail in that template that was
causing this issue. Here is the code below:<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkblue; font-family: "inconsolata"; font-size: 9.0pt;">function</span><span style="font-family: "inconsolata"; font-size: 9.0pt;"> <span style="color: blueviolet;">New-Function</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "inconsolata"; font-size: 9.0pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkgreen; font-family: "inconsolata"; font-size: 9.0pt;"><#<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkgreen; font-family: "inconsolata"; font-size: 9.0pt;">.SYNOPSIS<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkgreen; font-family: "inconsolata"; font-size: 9.0pt;">.EXAMPLE<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkgreen; font-family: "inconsolata"; font-size: 9.0pt;">New-Function -ComputerName server<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkgreen; font-family: "inconsolata"; font-size: 9.0pt;">.EXAMPLE<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkgreen; font-family: "inconsolata"; font-size: 9.0pt;">.NOTES<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkgreen; font-family: "inconsolata"; font-size: 9.0pt;">#></span><span style="font-family: "inconsolata"; font-size: 9.0pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "inconsolata"; font-size: 9.0pt;"> <span style="color: darkgrey;">[</span><span style="color: deepskyblue;">cmdletbinding</span>()<span style="color: darkgrey;">]</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "inconsolata"; font-size: 9.0pt;"> <span style="color: darkblue;">param</span>(<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "inconsolata"; font-size: 9.0pt;"> <span style="color: darkgreen;">#
Pipeline variable</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "inconsolata"; font-size: 9.0pt;"> <span style="color: darkgrey;">[</span><span style="color: deepskyblue;">Parameter</span>(<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "inconsolata"; font-size: 9.0pt;"> Mandatory <span style="color: darkgrey;">=</span> <span style="color: orangered;">$true</span><span style="color: darkgrey;">,</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "inconsolata"; font-size: 9.0pt;"> HelpMessage <span style="color: darkgrey;">=</span> <span style="color: darkred;">''</span><span style="color: darkgrey;">,</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "inconsolata"; font-size: 9.0pt;"> Position <span style="color: darkgrey;">=</span>
<span style="color: purple;">0</span><span style="color: darkgrey;">,</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "inconsolata"; font-size: 9.0pt;"> ValueFromPipeline <span style="color: darkgrey;">=</span> <span style="color: orangered;">$true</span><span style="color: darkgrey;">,</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "inconsolata"; font-size: 9.0pt;"> ValueFromPipelineByPropertyName <span style="color: darkgrey;">=</span> <span style="color: orangered;">$true</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "inconsolata"; font-size: 9.0pt;"> )<span style="color: darkgrey;">]</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "inconsolata"; font-size: 9.0pt;"> <span style="color: darkgrey;">[</span><span style="color: deepskyblue;">Alias</span>(<span style="color: darkred;">'Server'</span>)<span style="color: darkgrey;">]</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "inconsolata"; font-size: 9.0pt;"> <span style="color: darkgrey;">[</span><span style="color: teal;">string</span><span style="color: darkgrey;">[]]</span><span style="color: orangered;">$ComputerName</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "inconsolata"; font-size: 9.0pt;"> )<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "inconsolata"; font-size: 9.0pt;"> <span style="color: darkblue;">process</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "inconsolata"; font-size: 9.0pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "inconsolata"; font-size: 9.0pt;"> <span style="color: darkblue;">foreach</span>(<span style="color: orangered;">$node</span> <span style="color: darkblue;">in</span> <span style="color: orangered;">$ComputerName</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "inconsolata"; font-size: 9.0pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "inconsolata"; font-size: 9.0pt;"> <span style="color: blue;">Write-Verbose</span>
<span style="color: orangered;">$node</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "inconsolata"; font-size: 9.0pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "inconsolata"; font-size: 9.0pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "inconsolata"; font-size: 9.0pt;">}
<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
I start with this and fill in the rest of my code as I go.
The full error message is this:<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkgreen; font-family: "inconsolata"; font-size: 9.0pt;">Cannot process argument because the value of argument
"value" is not valid. Change the value of the "value"
argument and run the operation again.<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkgreen; font-family: "inconsolata"; font-size: 9.0pt;">At line:16 char:11<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkgreen; font-family: "inconsolata"; font-size: 9.0pt;">+ [Parameter(<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkgreen; font-family: "inconsolata"; font-size: 9.0pt;">+ ~~~~~~~~~~~<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkgreen; font-family: "inconsolata"; font-size: 9.0pt;"> + CategoryInfo : InvalidOperation: (:) [],
RuntimeException<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkgreen; font-family: "inconsolata"; font-size: 9.0pt;"> +
FullyQualifiedErrorId : PropertyAssignmentException <o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
The function also acts kind of strange. I can tab complete
the name but not any arguments. This tells me it is not loading correctly. If I
try and get-help on the command, I get the exact same error message. <o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
It took me a little trial and error but I narrowed it down
to one of the parameter attributes. In this case, if the HelpMessage was left '' then it would error out. I would usually remove this line or add a real message
eventually. <o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "inconsolata"; font-size: 12.0pt;"> </span><span style="font-family: "inconsolata"; font-size: 9.0pt;"> <span style="color: darkgreen;">#
Pipeline variable</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "inconsolata"; font-size: 9.0pt;"> <span style="color: darkgrey;">[</span><span style="color: deepskyblue;">Parameter</span>(<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "inconsolata"; font-size: 9.0pt;"> Mandatory <span style="color: darkgrey;">=</span> <span style="color: orangered;">$true</span><span style="color: darkgrey;">,</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "inconsolata"; font-size: 9.0pt;"> <b>HelpMessage <span style="color: darkgrey;">=</span> <span style="color: darkred;">''</span><span style="color: darkgrey;">,</span></b><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "inconsolata"; font-size: 9.0pt;"> Position <span style="color: darkgrey;">=</span>
<span style="color: purple;">0</span><span style="color: darkgrey;">,</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "inconsolata"; font-size: 9.0pt;"> ValueFromPipeline <span style="color: darkgrey;">=</span> <span style="color: orangered;">$true</span><span style="color: darkgrey;">,</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "inconsolata"; font-size: 9.0pt;"> ValueFromPipelineByPropertyName <span style="color: darkgrey;">=</span> <span style="color: orangered;">$true</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "inconsolata"; font-size: 9.0pt;"> )<span style="color: darkgrey;">]</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "inconsolata"; font-size: 9.0pt;"> <span style="color: darkgrey;">[</span><span style="color: deepskyblue;">Alias</span>(<span style="color: darkred;">'Server'</span>)<span style="color: darkgrey;">]</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "inconsolata"; font-size: 9.0pt;"> <span style="color: darkgrey;">[</span><span style="color: teal;">string</span><span style="color: darkgrey;">[]]</span><span style="color: orangered;">$ComputerName <o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
So if you are getting this error message, pay attention to
the actual values in the parameter. If any of them are blank or null, you may
run into this.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
In the end, I updated my snippet to use a different placeholder.
Problem solved.<o:p></o:p></div>
<br />
<div class="MsoNormal">
<br /></div>
Kevin Marquettehttp://www.blogger.com/profile/16389030891301082770noreply@blogger.com0tag:blogger.com,1999:blog-8683693.post-336888909173841582015-11-25T16:16:00.003-06:002015-11-25T16:16:55.398-06:00Here is my custom Powershell prompt<div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;">Get-Help</span><span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: blueviolet;">about_Prompts
<o:p></o:p></span></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkgreen; font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;"><#<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkgreen; font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;">LONG DESCRIPTION<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkgreen; font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;"> The Windows
PowerShell command prompt indicates that Windows PowerShell<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkgreen; font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;"> is ready to run a
command:<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkgreen; font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;"> PS C:\><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkgreen; font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;"> The Windows
PowerShell prompt is determined by the built-in Prompt<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkgreen; font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;"> function. You can
customize the prompt by creating your own Prompt<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkgreen; font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;"> function and saving
it in your Windows PowerShell profile.<o:p></o:p></span></div>
<div class="MsoNormal">
</div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkgreen; font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;">#></span><span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;"><o:p></o:p></span></div>
</div>
<div>
<br /></div>
<div>
One of my biggest issues with the default prompt is that I work with a lot of nested folders and network shares. It makes the path so long because the path is in there. So I change my prompt to just show the current folder and place the full path in the tittle bar.</div>
<div>
<br /></div>
<div>
One other thing I do is add basic command logging. I would use transcripts, but I don't want something that verbose. So I just save my last command to a text file whenever I run it.</div>
<div>
<br /></div>
<div>
The last thing I so is calculate where in the history the next command will be and add that to my prompt.</div>
<div>
<br /></div>
<div>
Here is my prompt function:</div>
<div>
<br /></div>
<div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: orangered; font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;">$PSLogPath</span><span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: darkgrey;">=</span>
(<span style="color: darkred;">"{0}{1}\Documents\WindowsPowerShell\log\{2:yyyyMMdd}-{3}.log"</span>
<span style="color: darkgrey;">-f</span> <span style="color: orangered;">$env:HOMEDRIVE</span><span style="color: darkgrey;">,</span> <span style="color: orangered;">$env:HOMEPATH</span><span style="color: darkgrey;">,</span> (<span style="color: blue;">Get-Date</span>)<span style="color: darkgrey;">,</span> <span style="color: orangered;">$PID</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;">Add-Content</span><span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: navy;">-Value</span> <span style="color: darkred;">"# </span>$(<span style="color: blue;">Get-Date</span>)<span style="color: darkred;"> </span><span style="color: orangered;">$env:username</span><span style="color: darkred;"> </span><span style="color: orangered;">$env:computername</span><span style="color: darkred;">"</span> <span style="color: navy;">-Path</span> <span style="color: orangered;">$PSLogPath</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;">Add-Content</span><span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: navy;">-Value</span> <span style="color: darkred;">"# </span>$(<span style="color: blue;">Get-Location</span>)<span style="color: darkred;">"</span> <span style="color: navy;">-Path</span> <span style="color: orangered;">$PSLogPath</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkblue; font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;">function</span><span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: blueviolet;">prompt</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: orangered;">$LastCmd</span>
<span style="color: darkgrey;">=</span> <span style="color: blue;">Get-History</span>
<span style="color: navy;">-Count</span> <span style="color: purple;">1</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: darkblue;">if</span>(<span style="color: orangered;">$LastCmd</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: orangered;">$lastId</span>
<span style="color: darkgrey;">=</span> <span style="color: orangered;">$LastCmd</span><span style="color: darkgrey;">.</span>Id<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: blue;">Add-Content</span>
<span style="color: navy;">-Value</span> <span style="color: darkred;">"# </span>$(<span style="color: orangered;">$LastCmd</span><span style="color: darkgrey;">.</span>StartExecutionTime)<span style="color: darkred;">"</span> <span style="color: navy;">-Path</span> <span style="color: orangered;">$PSLogPath</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: blue;">Add-Content</span>
<span style="color: navy;">-Value</span> <span style="color: darkred;">"</span>$(<span style="color: orangered;">$LastCmd</span><span style="color: darkgrey;">.</span>CommandLine)<span style="color: darkred;">"</span> <span style="color: navy;">-Path</span> <span style="color: orangered;">$PSLogPath</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: blue;">Add-Content</span>
<span style="color: navy;">-Value</span> <span style="color: darkred;">""</span>
<span style="color: navy;">-Path</span> <span style="color: orangered;">$PSLogPath</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: orangered;">$nextCommand</span>
<span style="color: darkgrey;">=</span> <span style="color: orangered;">$lastId</span>
<span style="color: darkgrey;">+</span> <span style="color: purple;">1</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: orangered;">$currentDirectory</span>
<span style="color: darkgrey;">=</span> <span style="color: blue;">Split-Path</span>
(<span style="color: blue;">Get-Location</span>) <span style="color: navy;">-Leaf</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: orangered;">$host</span><span style="color: darkgrey;">.</span>UI<span style="color: darkgrey;">.</span>RawUI<span style="color: darkgrey;">.</span>WindowTitle <span style="color: darkgrey;">=</span>
<span style="color: blue;">Get-Location</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: darkred;">"</span><span style="color: orangered;">$nextCommand</span><span style="color: darkred;"> PS:</span><span style="color: orangered;">$currentDirectory</span><span style="color: darkred;">>"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 9.0pt; mso-bidi-font-family: Inconsolata;">} <o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
Kevin Marquettehttp://www.blogger.com/profile/16389030891301082770noreply@blogger.com0tag:blogger.com,1999:blog-8683693.post-48547238314107760662015-11-16T07:40:00.000-06:002015-11-16T07:40:00.424-06:00Powershell: Script injection with ScriptBlock.CheckRestrictedLanguage MethodI just had a post about importing hashtables from files. It basically loads the file into a script block and executes it. <div>
<br /></div>
<div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: orangered; font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">$content</span><span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: darkgrey;">=</span> <span style="color: blue;">Get-Content</span> <span style="color: navy;">-Path</span> <span style="color: orangered;">$Path</span> <span style="color: navy;">-Raw</span> <span style="color: navy;">-ErrorAction</span> <span style="color: blueviolet;">Stop</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: orangered; font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">$scriptBlock</span><span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: darkgrey;">=</span>
<span style="color: darkgrey;">[</span><span style="color: teal;">scriptblock</span><span style="color: darkgrey;">]::</span>Create(<span style="color: orangered;">$content</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: orangered; font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">$scriptBlock</span><span style="color: darkgrey; font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">.</span><span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">CheckRestrictedLanguage(<span style="color: darkgrey;">[</span><span style="color: teal;">string</span><span style="color: darkgrey;">[]]</span>@()<span style="color: darkgrey;">,</span> <span style="color: darkgrey;">[</span><span style="color: teal;">string</span><span style="color: darkgrey;">[]]</span>@()<span style="color: darkgrey;">,</span> <span style="color: orangered;">$false</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">Write-Output</span><span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> (<span style="color: darkgrey;">&</span> <span style="color: orangered;">$scriptBlock</span>) <o:p></o:p></span></div>
</div>
<div>
<br /></div>
<div>
If that sounds dangerous, that is because it is. Your module or script my be trusted, but it may be loading files that are not trusted. This could be a stealthy way for an attacker to use your script.</div>
<div>
<br /></div>
<div>
I took the extra step of using <span style="color: orangered; font-family: Inconsolata; font-size: 12pt;">$scriptBlock</span><span style="color: darkgrey; font-family: Inconsolata; font-size: 12pt;">.</span><span style="font-family: Inconsolata; font-size: 12pt;">CheckRestrictedLanguage(<span style="color: darkgrey;">[</span><span style="color: teal;">string</span><span style="color: darkgrey;">[]]</span>@()<span style="color: darkgrey;">,</span> <span style="color: darkgrey;">[</span><span style="color: teal;">string</span><span style="color: darkgrey;">[]]</span>@()<span style="color: darkgrey;">,</span> <span style="color: orangered;">$false</span>) to make sure the hashtable is not containing Powershell commands. There is one important gotcha to be aware of with this command. The arguments are not intuitive. </span></div>
<div>
<span style="font-family: Inconsolata; font-size: 12pt;"><br /></span></div>
<div>
<span style="font-family: Inconsolata; font-size: 12pt;">Lets take this command. Here is how the function is defined using a C# sample:</span></div>
<div>
<span style="font-family: Inconsolata; font-size: 12pt;"><br /></span></div>
<div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in; mso-line-height-alt: 8.8pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-fareast-font-family: "Times New Roman";">public</span><span style="font-family: 'Courier New'; font-size: 10pt;">:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in; mso-line-height-alt: 8.8pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-fareast-font-family: "Times New Roman";">void</span><span style="font-family: 'Courier New'; font-size: 10pt;"> CheckRestrictedLanguage
(<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in; mso-line-height-alt: 8.8pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;">
<span style="font-family: 'Courier New'; font-size: 10pt;"> IEnumerable<string>
allowedCommands, <o:p></o:p></string></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in; mso-line-height-alt: 8.8pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;">
<span style="font-family: 'Courier New'; font-size: 10pt;"> IEnumerable<string>
allowedVariables, <o:p></o:p></string></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in; mso-line-height-alt: 8.8pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;">
<span style="font-family: 'Courier New'; font-size: 10pt;"> </span><span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-fareast-font-family: "Times New Roman";">bool</span><span style="font-family: 'Courier New'; font-size: 10pt;">
allowEnvironmentVariables<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0in; mso-line-height-alt: 8.8pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;">
<span style="font-family: 'Courier New'; font-size: 10pt;">)<o:p></o:p></span></div>
</div>
<div>
<span style="font-family: Inconsolata; font-size: 12pt;"><br /></span></div>
<div>
<span style="font-family: Inconsolata; font-size: 12pt;">The first argument is the allowed commands and the second is the allowed variables. One could reasonably assume that a $null value for the allowed commands would mean that nothing is allowed.</span></div>
<div>
<span style="font-family: Inconsolata; font-size: 12pt;"><br /></span></div>
<div>
<span style="font-family: Inconsolata; font-size: 12pt;">If you look at my code, I create an empty string array. That may look like a very strange thing to do and I kind of agree. This is because a $null value indicates that it should allow some default commands to execute. The only way to know this one is to read the documentation very closely. </span><span style="font-family: Inconsolata;">https://msdn.microsoft.com/en-us/library/system.management.automation.scriptblock.checkrestrictedlanguage(v=vs.85).aspx?cs-save-lang=1&cs-lang=cpp#code-snippet-2</span></div>
<div>
<span style="font-family: Inconsolata;"><br /></span></div>
<div>
<span style="font-family: Inconsolata;">By using an empty list of strings, I do not allow any Powershell commands. When importing a hashtable, this is exactly what I want.</span></div>
Kevin Marquettehttp://www.blogger.com/profile/16389030891301082770noreply@blogger.com0tag:blogger.com,1999:blog-8683693.post-13024713057854251652015-11-08T19:16:00.000-06:002015-11-08T19:20:44.961-06:00Powershell: Importing hashtable from file or a psd1 fileHave you ever wanted to import a hashtable from a file? A module manifest that is saved in a *.psd1 file is a hashtable. If you ever wanted to read the meta data in it, this trick may help.<br />
<br />
You import the contents into a script block, validate the script block, execute it and capture the resulting hashtable into a variable. Here is the sample code below:<br />
<span style="background-color: white; font-family: "inconsolata"; font-size: 12pt;"><br /></span>
<br />
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: orangered; font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">$content</span><span style="font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: darkgrey;">=</span> <span style="color: blue;">Get-Content</span> <span style="color: navy;">-Path</span> <span style="color: orangered;">$Path</span> <span style="color: navy;">-Raw</span> <span style="color: navy;">-ErrorAction</span> <span style="color: blueviolet;">Stop</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: orangered; font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">$scriptBlock</span><span style="font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: darkgrey;">=</span>
<span style="color: darkgrey;">[</span><span style="color: teal;">scriptblock</span><span style="color: darkgrey;">]::</span>Create(<span style="color: orangered;">$content</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: orangered; font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">$scriptBlock</span><span style="color: darkgrey; font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">.</span><span style="font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">CheckRestrictedLanguage(<span style="color: darkgrey;">[</span><span style="color: teal;">string</span><span style="color: darkgrey;">[]]</span>@()<span style="color: darkgrey;">,</span> <span style="color: darkgrey;">[</span><span style="color: teal;">string</span><span style="color: darkgrey;">[]]</span>@()<span style="color: darkgrey;">,</span> <span style="color: orangered;">$false</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">Write-Output</span><span style="font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> (<span style="color: darkgrey;">&</span> <span style="color: orangered;">$scriptBlock</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<br /></div>
<br />
<div class="MsoNormal">
<br /></div>
<span style="background-color: white; font-family: "inconsolata"; font-size: 12pt;">If you target a module manifest, you can access all the </span><span style="background-color: white; font-family: "inconsolata";">attributes</span><span style="background-color: white; font-family: "inconsolata"; font-size: 12pt;"> in it. </span><br />
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"><br /></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkgreen; font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">Name
Value<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkgreen; font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">----
-----<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkgreen; font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">Copyright
(c) 2015 Kevin.Marquette. All rights reserved.<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkgreen; font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">CompanyName
Self<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkgreen; font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">GUID
6ab379f9-41ed-4c1e-beda-7855d1c1e3c8<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkgreen; font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">Author
Kevin.Marquette<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkgreen; font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">FunctionsToExport
*<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkgreen; font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">VariablesToExport
*<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkgreen; font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">RootModule
.\my_module.psm1<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkgreen; font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">AliasesToExport
*<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkgreen; font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">CmdletsToExport
*<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
</div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkgreen; font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">ModuleVersion
1.0.1 <o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"><br /></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "inconsolata";">The CheckRestrictedLanguage will throw an error if it finds any powershell commands in the hashtable. Because you are executing code from a un-trusted source in the middle of your script, you should validate it.</span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "inconsolata";"><br /></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "inconsolata";"><span style="font-size: 12pt;">There is a second quick and dirty way to do the same thing without the validation. I almost don't want to mention it because it is so </span>dangerous<span style="font-size: 12pt;">. So if you see this in the wild, know that there is a better way.</span></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "inconsolata";"><span style="font-size: 12pt;"><br /></span></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: orangered; font-family: "inconsolata"; font-size: 12pt;">$HashTable</span><span style="font-family: "inconsolata"; font-size: 12pt;"> <span style="color: darkgrey;">=</span>
<span style="color: blue;">Invoke-Expression</span> (<span style="color: blue;">Get-Content</span>
<span style="color: orangered;">$Path</span> <span style="color: navy;">-raw</span>)</span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "inconsolata";">This blindly executes a file as if it was a script. This is just asking to be exploited. Think CSS cross site or SQL injection type of vulnerability. </span></div>
Kevin Marquettehttp://www.blogger.com/profile/16389030891301082770noreply@blogger.com0tag:blogger.com,1999:blog-8683693.post-60871410926165763822015-10-26T21:53:00.001-05:002015-10-26T21:59:35.739-05:00Using Powershell to unlock Active Directory account Unlock-ADAccountOK, this is an easy one. There is a command in the ActiveDirectory module that already takes care of this.<br />
<br />
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">Unlock-ADAccount</span><span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: blueviolet;">kevmar <o:p></o:p></span></span></div>
<br />
So quick to type and it works so well. But unlocking the account is only part of the issue. The real question is why is it locked. If your lucky, the user will say they just miss-typed it a few times and you can let it go. Sometimes you find an account that just locks over and over. Here is a simple command that works really well in my environment to find the source of the issue:<br />
<br />
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkblue; font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">function</span><span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: blueviolet;">Get-ADAccountLockInfo</span>
<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: darkgrey;">[</span><span style="color: deepskyblue;">cmdletbinding</span>()<span style="color: darkgrey;">]</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: darkblue;">param</span>(<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: orangered;">$ComputerName</span>
<span style="color: darkgrey;">=</span> <span style="color: darkred;">"servername"</span><span style="color: darkgrey;">,</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: darkgrey;">[</span><span style="color: teal;">pscredential</span><span style="color: darkgrey;">]</span> <span style="color: orangered;">$Credential</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> )<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: orangered;">$WinEvent</span>
<span style="color: darkgrey;">=</span> @{<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> ComputerName <span style="color: darkgrey;">=</span>
<span style="color: orangered;">$ComputerName</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> FilterHashtable <span style="color: darkgrey;">=</span> @{logname<span style="color: darkgrey;">=</span><span style="color: darkred;">'security'</span>;id<span style="color: darkgrey;">=</span><span style="color: purple;">4740</span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: darkblue;">if</span>(<span style="color: orangered;">$Credential</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: orangered;">$WinEvent</span><span style="color: darkgrey;">.</span>Credential <span style="color: darkgrey;">=</span>
<span style="color: orangered;">$Credential</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: blue;">Get-WinEvent</span>
<span style="color: orangered;">@WinEvent</span> <span style="color: darkgrey;">|</span>
<span style="color: blue;">%</span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: darkgrey;">[</span><span style="color: teal;">pscustomobject</span><span style="color: darkgrey;">]</span>@{<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> TimeCreated <span style="color: darkgrey;">=</span> <span style="color: orangered;">$_</span><span style="color: darkgrey;">.</span>timecreated;
<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> Username <span style="color: darkgrey;">=</span> <span style="color: orangered;">$_</span><span style="color: darkgrey;">.</span>properties<span style="color: darkgrey;">[</span><span style="color: purple;">0</span><span style="color: darkgrey;">].</span>value; <o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> ComputerName <span style="color: darkgrey;">=</span> <span style="color: orangered;">$_</span><span style="color: darkgrey;">.</span>properties<span style="color: darkgrey;">[</span><span style="color: purple;">1</span><span style="color: darkgrey;">].</span>value<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">}
<o:p></o:p></span></div>
<br />
<div class="MsoNormal">
<br /></div>
Replace the server name with the name of the domain controller that is the PDC emulator. If you do not know what one that is, you can run this once for each domain controller. What it does is queries the security log for event id <a href="http://blogs.technet.com/b/heyscriptingguy/archive/2012/12/27/use-powershell-to-find-the-location-of-a-locked-out-user.aspx" target="_blank">4740</a>. It then tells you who is locked at what time and where the source is.<br />
<br />
I added a credentials option because I often execute this cross domain. The $WinEvent is a hashtable that I splat to the Get-WinEvent. That makes it easy for me to add the credential to the hashtable when if it is defined.<br />
<br />
Any idea how to troubleshoot this with out knowing the event ID to look for? You start with the cell phone and clear any WiFi settings and emails settings. Then do the same for any tablets they have (even at home). Then exit lync and outlook on their laptop and desktop. Open 'Credential Manager' and delete all the saved credentials (do a start menu search for it). Then check all desktops and servers where they may have used RDP to connect to a session but just disconnected when they are done. Then check for services, jobs, tasks, and reporting services connections that may be using that credential.<br />
<br />
Yeh, searching the security log is much easier.Kevin Marquettehttp://www.blogger.com/profile/16389030891301082770noreply@blogger.com1tag:blogger.com,1999:blog-8683693.post-75438762136825808312015-10-10T19:38:00.002-05:002015-10-10T19:38:58.200-05:00Powershell: how to return value from function?Returning a value from a function is really easy in Powershell. Sometimes it is too easy if you are not careful about it. One important detail to keep in mind is that Powershell is built around placing objects on the pipe. This makes functions work differently in Powershell than it does from different languages. You are also not limited to just one return value.<div>
<br /></div>
<div>
Here is a minimal function:</div>
<div>
<br /></div>
<div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkblue; font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">function</span><span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: blueviolet;">Test-Function</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: darkred;">"Hello
World!"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">} <o:p></o:p></span></div>
</div>
<div>
<br /></div>
<div>
Calling this places a string of "Hello World!" onto the pipe. You can catch it in a variable if you want or pipe it to some other command. The better way to do it is to use the Write-Output cmdlet. It makes it more clear as to what you are doing do debugging and troubleshooting become easier. </div>
<div>
<br /></div>
<div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkblue; font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">function</span><span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: blueviolet;">Test-Function</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: blue;">Write-Output</span>
<span style="color: darkred;">"Hello World!"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">}
<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
You can call Write-Output multiple times in a function to return multiple items. You could place it into a loop that runs 10 times and it will output 10 objects. I mention this because Powershell also supports the return keyword but it acts different. When a function gets to any return statement, it exits the function. </div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkblue; font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">function</span><span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: blueviolet;">Test-Function</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: darkblue;">return</span> <span style="color: darkred;">"Hello World!"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">}
<o:p></o:p></span></div>
<div class="MsoNormal">
</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
I prefer Write-Output over return but do use return when it is needed. </div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
I also want to mention that Write-Host is something that you see in scripts from time to time, but it is for host only output. So it is hard to capture into a variable and does not go into the pipe. In most cases, Write-Output is the better choice.</div>
</div>
Kevin Marquettehttp://www.blogger.com/profile/16389030891301082770noreply@blogger.com0tag:blogger.com,1999:blog-8683693.post-52846747989849548992015-10-10T19:20:00.000-05:002015-10-10T19:20:04.453-05:00Powershell: how to call a function?Sometimes it is the simplest thing that can be the most frustrating when learning a new language. Calling a custom function in Powershell can be one of those if you come from a different language. If you are having an issue with this, you have come to the right place. You are probably looking at something like this and wondering why it wont work:
<br />
<br />
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">TestFunction</span><span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">(<span style="color: orangered;">$Name</span><span style="color: darkgrey;">,</span> <span style="color: orangered;">$Count</span>) <o:p></o:p></span></div>
<br />
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<o:p>If your coming from another language, this looks like it would be perfectly normal. The gotcha in Powershell is that the coma and the parenthesis are not needed here for a function call and actually mean something else. In this example it defines an array.</o:p></div>
<div class="MsoNormal">
<o:p> </o:p></div>
<div class="MsoNormal">
Before I go any further, let's define TestFunction:</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkblue; font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">function</span><span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: blueviolet;">TestFunction</span>
(<span style="color: orangered;">$name</span><span style="color: darkgrey;">,</span>
<span style="color: orangered;">$count</span>) <o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: blue;">Write-Output</span>
<span style="color: darkred;">"Name is '</span><span style="color: orangered;">$name</span><span style="color: darkred;">' and Count is '</span><span style="color: orangered;">$count</span><span style="color: darkred;">'"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">}
<o:p></o:p></span></div>
<div class="MsoNormal">
</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<o:p>It is a simple function that takes two parameters. While this works as a function and looks like something you know, let's do it the Powershell way:</o:p></div>
<div class="MsoNormal">
<o:p><br /></o:p></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkblue; font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">function</span><span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: blueviolet;">TestFunction</span>
<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: darkblue;">param</span>(<span style="color: orangered;">$name</span><span style="color: darkgrey;">,</span> <span style="color: orangered;">$count</span>) <o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: blue;">Write-Output</span>
<span style="color: darkred;">"Name is '</span><span style="color: orangered;">$name</span><span style="color: darkred;">' and Count is '</span><span style="color: orangered;">$count</span><span style="color: darkred;">'"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">}
<o:p></o:p></span></div>
<div class="MsoNormal">
<o:p>
</o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
So now we have a function that takes two arguments. The first argument is name and the second argument is count. Here are the correct ways to call that function:</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">TestFunction</span><span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: orangered;">$Name</span> <span style="color: orangered;">$Count</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">TestFunction</span><span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: navy;">-name</span> <span style="color: orangered;">$Name</span> <span style="color: navy;">-count</span> <span style="color: orangered;">$Count</span><o:p></o:p></span></div>
<div class="MsoNormal">
</div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">TestFunction</span><span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: navy;">-name</span> <span style="color: darkred;">"Kevin"</span> <span style="color: navy;">-count</span>
<span style="color: purple;">1 <o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Now that we know the correct way to make that call and know what the function looks like, I can explain why our initial call fails to work. Because it is the equivalent to this call:</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: orangered; font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">$Array</span><span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: darkgrey;">=</span> (<span style="color: orangered;">$Name</span><span style="color: darkgrey;">,</span><span style="color: orangered;">$Count</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">TestFunction</span><span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: navy;">-name</span> <span style="color: orangered;">$Array</span> <span style="color: navy;">-count</span> <span style="color: orangered;">$null <o:p></o:p></span></span></div>
<div class="MsoNormal">
</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
So you were basically creating an array that contained your two variables and passing it in as an array to the first argument. You were also leaving the second argument as a null value. Now that you see it, it looks like a silly thing to do. No wonder things were not working as you expected. </div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
Kevin Marquettehttp://www.blogger.com/profile/16389030891301082770noreply@blogger.com0tag:blogger.com,1999:blog-8683693.post-3717214766586004772015-09-28T19:41:00.000-05:002015-10-10T19:42:16.747-05:00Powershell: how to concatenate strings?I was going to write a post on how to concatenate or join string, but the <a href="http://blogs.technet.com/b/heyscriptingguy/archive/2014/07/15/keep-your-hands-clean-use-powershell-to-glue-strings-together.aspx" target="_blank">Hey, Scripting Guy! Blog</a> already has a great write up on that.Kevin Marquettehttp://www.blogger.com/profile/16389030891301082770noreply@blogger.com0tag:blogger.com,1999:blog-8683693.post-8477344947763121212015-06-22T20:57:00.000-05:002015-11-08T22:03:20.912-06:00Quick and Dirty Powershell ModulesSo you have built some awesome scripts and turned them into advanced functions. Whats next? It is time to put them into a module. It can be a lot easier than you realize. Lets go step by step and build our first module to hold all of your advanced functions.<br />
<iframe allowfullscreen="" frameborder="0" height="315" src="https://www.youtube.com/embed/q8NZM7r-ddo" width="560"></iframe>
<br />
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkgreen; font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"># Start in our profile powershell folder</span><span style="font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"><span style="color: blue;">CD</span> <span style="color: blueviolet;">~\Documents\WindowsPowershell</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkgreen; font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"># Create a folder for our module and functions</span><span style="font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">MD</span><span style="font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: blueviolet;">Modules\Other\Functions</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkgreen; font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"># Create a module manifest </span><span style="font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: orangered; font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">$Manifest</span><span style="font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: darkgrey;">=</span>
@{<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> Path
<span style="color: darkgrey;">=</span> <span style="color: darkred;">".\Modules\Other\Other.psd1"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> RootModule
<span style="color: darkgrey;">=</span> <span style="color: darkred;">".\Other.psm1"</span>
<span style="color: darkgreen;"># Module loader</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> Author
<span style="color: darkgrey;">=</span> <span style="color: darkred;">"Kevin
Marquette"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> Description <span style="color: darkgrey;">=</span>
<span style="color: darkred;">"Odds and ends"</span> <o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">New-ModuleManifest</span><span style="font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: orangered;">@Manifest</span>
<span style="color: navy;">-Verbose</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkgreen; font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"># Create our module loader (that loads our advanced functions)</span><span style="font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: orangered; font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">$ModuleLoader</span><span style="font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: darkgrey;">=</span>
<span style="color: darkred;">@'<o:p></o:p></span></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkred; font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> $moduleRoot = Split-Path
-Path $MyInvocation.MyCommand.Path<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkred; font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> Write-Verbose
"Importing Functions"<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkred; font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> # Import everything in
the functions folder<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkred; font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">
"$moduleRoot\Functions\*.ps1" |<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkred; font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> Resolve-Path |<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkred; font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> Where-Object { -not
($_.ProviderPath.Contains(".Tests.")) } |<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkred; font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> ForEach-Object { .
$_.ProviderPath ; Write-Verbose $_.ProviderPath}<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkred; font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">'@</span><span style="font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">Set-Content</span><span style="font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: navy;">-Value</span> <span style="color: orangered;">$ModuleLoader</span> <span style="color: navy;">-Path</span>
<span style="color: blueviolet;">.\Modules\Other\Other.psm1</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkgreen; font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"># Now create a single file for each advanced function and
place it in the functions folder</span><span style="font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkgreen; font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"># Sample function</span><span style="font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: orangered; font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">$TestFunction</span><span style="font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: darkgrey;">=</span>
<span style="color: darkred;">@'<o:p></o:p></span></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkred; font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> function Test-Other<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkred; font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkred; font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> [cmdletbinding()]<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkred; font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> param()<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkred; font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> Write-Output
"Hello World!!"<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkred; font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkred; font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">'@</span><span style="font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">Set-Content</span><span style="font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: navy;">-Value</span> <span style="color: orangered;">$TestFunction</span> <span style="color: navy;">-Path</span>
<span style="color: blueviolet;">.\Modules\Other\functions\Test-Other.ps1</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkgreen; font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"># Load it and test it out</span><span style="font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">Import-Module</span><span style="font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;"> <span style="color: blueviolet;">Other</span> <span style="color: navy;">-Verbose</span> <span style="color: navy;">-Force</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "inconsolata"; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">Test-Other <o:p></o:p></span></div>
<br />
<div class="MsoNormal">
<br /></div>
You could easily place all your functions into the Other.psm1 file and everything would still work. But this creates a framework that makes your functions easy to manage. That functions folder can now be a dumping ground for all your advanced functions. If you out grow this module, you can create a new one and just move the functions over.<br />
<br />
The module loader is what enables that. Later we will add pester tests and this already accounts for this. This is a pattern that I seen used by other Powershell MVPs. It has greatly simplified my function management.Kevin Marquettehttp://www.blogger.com/profile/16389030891301082770noreply@blogger.com0tag:blogger.com,1999:blog-8683693.post-89844140379986135362015-06-05T06:59:00.000-05:002015-06-05T06:59:00.678-05:00JoinDomainOrWorkgroup 1323 error unable to update the passwordI am using the Win32_ComputerSystem WMI object to join machines to a domain with <a href="https://msdn.microsoft.com/en-us/library/aa392154%28v=vs.85%29.aspx" target="_blank">JoinDomainOrWorkgroup</a>. Then I ran into an issue on XP/2003 where I would get this 1323 return code and it would fail. This looked to work find for server 2012 so I started digging.<br />
<br />
From the <a href="https://msdn.microsoft.com/en-us/library/aa392154%28v=vs.85%29.aspx" target="_blank">MSDN documentation</a>, error 1323 means "<b>error unable to update the password</b>". My first set of searches implied that the time was out of sync between the servers. Manual join worked fine and after syncing the time to the DC before the join, I had the same issue.<br />
<br />
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: orangered; font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">$wmi</span><span style="color: darkgrey; font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">.</span><span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">JoinDomainOrWorkgroup(<span style="color: darkred;">"Domain"</span><span style="color: darkgrey;">,</span><span style="color: darkred;">"Password"</span><span style="color: darkgrey;">,</span><span style="color: darkred;">"UserName"</span><span style="color: darkgrey;">,</span><span style="color: orangered;">$null</span><span style="color: darkgrey;">,</span><span style="color: purple;">3</span>) <o:p></o:p></span></div>
<br />
Then I found a comment <a href="http://microsoft.public.win32.programmer.wmi.narkive.com/M3H1Lo46/joindomainorworkgroup" target="_blank">here</a> that said add the domain to the username.<br />
<br />
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: orangered; font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">$wmi</span><span style="color: darkgrey; font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">.</span><span style="font-family: Inconsolata; font-size: 12.0pt; mso-bidi-font-family: Inconsolata;">JoinDomainOrWorkgroup(<span style="color: darkred;">"Domain"</span><span style="color: darkgrey;">,</span><span style="color: darkred;">"Password"</span><span style="color: darkgrey;">,</span><span style="color: darkred;">"Domain\UserName"</span><span style="color: darkgrey;">,</span><span style="color: orangered;">$null</span><span style="color: darkgrey;">,</span><span style="color: purple;">3</span>) <o:p></o:p></span></div>
<br />
And then it worked.Kevin Marquettehttp://www.blogger.com/profile/16389030891301082770noreply@blogger.com1tag:blogger.com,1999:blog-8683693.post-27064660963269391952015-05-25T06:58:00.000-05:002015-05-25T06:58:00.075-05:00Solving the hardest problems with powershell. Where to go next?<div class="MsoNormal">
Sometimes you run into a problem and you don’t know where to
go next. When I run into an issue, it often becomes an obsession for me to
solve it. It is easy to say google it, but sometimes you need to be able to
look at your problem from different points of view for google to actually help
you. I have been wiring Powershell for a long time and this is how I tackle
those hard to solve problems.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
First use get-help, get-command, show-command, get-member,
and Format-List * to try and discover a command and get information about
object. The more advanced you get with Powershell, the more you will use these
commands. So build that habit. (Run update-help to get the most recent
updates).<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Then turn to google. Something like “Powershell thing I am
trying to do”. There are a lot of good samples and old samples out there. The
old samples will still work but you may miss the new way to solve that problem
if you are on server 2012/windows 8. This is why those first commands are
important. If you want examples, get-help command –examples.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Then search for a command line way to do it. If a command or
a tool exists, then that’s the easiest way. Depending on what I find, I may
look at other solutions and come back to this one. <o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
If this is a standalone application, figure out how it
stores its settings. Registry, text file or database. Knowing this will give
you a direction. I’ll mention how to flesh these out later if you are unsure at
this point.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Then search for a WMI solution. Powershell and WMI play
really well together. Windows is an API based OS and a lot of those APIs are
exposed in WMI or CIM. People have been working with WMI for a long time and
lots of examples exists. I solve a lot of problems with WMI. One I know the
object to look at, get-member and format-list * help me explore it. There is
also a show-object script floating around that may also help here.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Then google for how to solve your problem with the registry.
The registry really controls a lot of things and I would bet a lot of the Powershell
script for system configuration are just setting a value in the registry if it
is not using WMI. <o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
From here, I look for a VBScript or C#/.Net solution. If it
feels like something an admin should be able to do, I will search harder for
VBscript. Odds are that someone has solved this issue before. VBScript is easy
to translate into simpler Powershell once you have done it a few times. C# and
.Net offer a lot of power but you may be diving into some serious code at this
point. You can take this to an extreme and look for Win32 API or system calls (rundll32
type stuff). This is never fun but sometimes that is where the solution is.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Another approach is using sysinternals to figure out how the
system does what it is doing. Procmon is great. It watches every file change
and registry change that your system does. So fire that up, and make your
change. Then start hunting for what the system did when you make that change.
If SQL is involved, then SQL profiler is also a must have tool.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Look for a GPO solution. If you can find one, remember that
most of group policy is just setting registry keys. If there is an ADM file,
you can dig into that for the actual key.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
While you are doing your hunting, there are a few
complications to be aware of. Is it a per user setting or a per machine
setting. User profiles can easily be adjusted by the user, but if you are
remote or running with different creds, there are often other roadblocks to
deal with.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<br />
<div class="MsoNormal">
Also, there is nothing wrong with asking for help either. I
just wanted to give you a direction. Even if this is all gibberish today. The
more advanced you get at this, the deeper down the rabbit hole you can go. I have
used every one of these techniques at some point to solve a problem.<o:p></o:p></div>
Kevin Marquettehttp://www.blogger.com/profile/16389030891301082770noreply@blogger.com0tag:blogger.com,1999:blog-8683693.post-88939779227741694302014-12-02T00:21:00.003-06:002014-12-02T00:22:08.960-06:00Using Pester to validate DSC resources and configurations Part 2<div class="MsoNormal">
Pester tests are like any other script. They grow and evolve
over time. Here are a few more tests that I have testing my DSC resources and
configurations that I recently added to my collection. <o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Does every resource
have a pester test?<o:p></o:p></b></div>
<div class="MsoNormal">
This is probably one of the most important tests I have.
Every resource should have a test, so why not test for that?<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";">describe</span><span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: darkred;">"DSCResources
located in </span><span style="color: orangered;">$PSScriptRoot</span><span style="color: darkred;">\DSCResources"</span> {<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: darkblue;">foreach</span>(<span style="color: orangered;">$Resource</span> <span style="color: darkblue;">in</span>
<span style="color: orangered;">$ResourceList</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">context</span> <span style="color: orangered;">$Resource</span><span style="color: darkgrey;">.</span>name
{<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">it</span> <span style="color: darkred;">"Has a pester test"</span> {<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> (<span style="color: orangered;">$Resource</span><span style="color: darkgrey;">.</span>fullname <span style="color: darkgrey;">+</span> <span style="color: darkred;">"\*.test.ps1"</span>) <span style="color: darkgrey;">|</span>
<span style="color: blue;">should</span> <span style="color: blueviolet;">exist</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> } <o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>If it is a standard
resource, does it have the files it needs?<o:p></o:p></b></div>
<div class="MsoNormal">
Each DSC resource needs to have two files in it. A *.psm1
file and a *.schema.mof file. I use the *.psm1 file as a quick way to identify
standard resources differently than a composite resource. I know I will not
ever reach a test condition that would cause once of these to fail, but I left
it in place so I could change the logic later.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: darkblue; font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";">if</span><span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";">(<span style="color: blue;">Test-Path</span>
(<span style="color: orangered;">$Resource</span><span style="color: darkgrey;">.</span>fullname
<span style="color: darkgrey;">+</span> <span style="color: darkred;">"\</span><span style="color: orangered;">$Resource</span><span style="color: darkred;">.psm1"</span>))<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";">{<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">it</span> <span style="color: darkred;">"Has a </span><span style="color: orangered;">$Resource</span><span style="color: darkred;">.schema.mof"</span> {<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> (<span style="color: orangered;">$Resource</span><span style="color: darkgrey;">.</span>fullname
<span style="color: darkgrey;">+</span> <span style="color: darkred;">"\</span><span style="color: orangered;">$Resource</span><span style="color: darkred;">.schema.mof"</span>)
<span style="color: darkgrey;">|</span> <span style="color: blue;">should</span> <span style="color: blueviolet;">exist</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">it</span> <span style="color: darkred;">"Has a </span><span style="color: orangered;">$Resource</span><span style="color: darkred;">.psm1"</span> {<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> (<span style="color: orangered;">$Resource</span><span style="color: darkgrey;">.</span>fullname
<span style="color: darkgrey;">+</span> <span style="color: darkred;">"\</span><span style="color: orangered;">$Resource</span><span style="color: darkred;">.psm1"</span>)
<span style="color: darkgrey;">|</span> <span style="color: blue;">should</span> <span style="color: blueviolet;">exist</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> } <o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Does it pass
Test-xDscSchema and Test-xDscResource tests?<o:p></o:p></b></div>
<div class="MsoNormal">
I may as well test for these as part of my pester tests.
They already validate a lot of things that are easy to overlook.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";">it</span><span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: darkred;">"Passes
Test-xDscSchema *.schema.mof"</span> {<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">Test-xDscSchema</span>
(<span style="color: orangered;">$Resource</span><span style="color: darkgrey;">.</span>fullname
<span style="color: darkgrey;">+</span> <span style="color: darkred;">"\</span><span style="color: orangered;">$Resource</span><span style="color: darkred;">.schema.mof"</span>)
<span style="color: darkgrey;">|</span> <span style="color: blue;">should</span> <span style="color: blueviolet;">be</span> <span style="color: blueviolet;">true</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";">}<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";">it</span><span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: darkred;">"Passes
Test-xDscResource"</span> {<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">Test-xDscResource</span>
<span style="color: orangered;">$Resource</span><span style="color: darkgrey;">.</span>fullname
<span style="color: darkgrey;">|</span> <span style="color: blue;">should</span> <span style="color: blueviolet;">be</span> <span style="color: blueviolet;">true</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";">}
<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>If it is a composite resource,
does it have the required files?<o:p></o:p></b></div>
<div class="MsoNormal">
A composite resource uses different files than a standard
resource. It has a *.psd1 and a *.shema.psm1 that should exists. I don’t have
any Test-xDSC functions for the composite resources so I add a few extra
checks. I verify that the *.psd1 file references the *.psm1 and that the module
does not throw any errors when dot sourcing it.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";">else</span><span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";">{<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">it</span> <span style="color: darkred;">"Has a </span><span style="color: orangered;">$Resource</span><span style="color: darkred;">.schema.psm1"</span> {<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> (<span style="color: orangered;">$Resource</span><span style="color: darkgrey;">.</span>fullname <span style="color: darkgrey;">+</span> <span style="color: darkred;">"\</span><span style="color: orangered;">$Resource</span><span style="color: darkred;">.schema.psm1"</span>) <span style="color: darkgrey;">|</span>
<span style="color: blue;">should</span> <span style="color: blueviolet;">exist</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">it</span> <span style="color: darkred;">"Has a </span><span style="color: orangered;">$Resource</span><span style="color: darkred;">.psd1"</span> {<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> (<span style="color: orangered;">$Resource</span><span style="color: darkgrey;">.</span>fullname <span style="color: darkgrey;">+</span> <span style="color: darkred;">"\</span><span style="color: orangered;">$Resource</span><span style="color: darkred;">.psd1"</span>) <span style="color: darkgrey;">|</span>
<span style="color: blue;">should</span> <span style="color: blueviolet;">exist</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">it</span> <span style="color: darkred;">"Has a psd1 that loads the schema.psm1"</span>
{<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> (<span style="color: orangered;">$Resource</span><span style="color: darkgrey;">.</span>fullname <span style="color: darkgrey;">+</span> <span style="color: darkred;">"\</span><span style="color: orangered;">$Resource</span><span style="color: darkred;">.psd1"</span>) <span style="color: darkgrey;">|</span>
<span style="color: blue;">should</span> <span style="color: blueviolet;">contain</span>
<span style="color: darkred;">"</span><span style="color: orangered;">$Resource</span><span style="color: darkred;">.schema.psm1"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">it</span> <span style="color: darkred;">"dot-sourcing should not throw an error"</span>
{<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: orangered;">$path</span> <span style="color: darkgrey;">=</span> (<span style="color: orangered;">$Resource</span><span style="color: darkgrey;">.</span>fullname <span style="color: darkgrey;">+</span> <span style="color: darkred;">"\</span><span style="color: orangered;">$Resource</span><span style="color: darkred;">.schema.psm1"</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> { <span style="color: blue;">Invoke-expression</span>
(<span style="color: blue;">Get-Content</span> <span style="color: orangered;">$path</span>
<span style="color: navy;">-raw</span>) } <span style="color: darkgrey;">|</span> <span style="color: blue;">should</span> <span style="color: blueviolet;">not</span> <span style="color: blueviolet;">throw</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> } <o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<br />
<div class="MsoNormal">
I hope you find this examples useful. If you want to see more, take a look at <a href="http://kevinmarquette.blogspot.com/2014/11/using-pester-to-validate-dsc-resources.html" target="_blank">part 1</a>.<o:p></o:p></div>
Kevin Marquettehttp://www.blogger.com/profile/16389030891301082770noreply@blogger.com0tag:blogger.com,1999:blog-8683693.post-65731242847794990902014-11-25T21:46:00.000-06:002014-11-25T21:48:31.779-06:00Setting HKey_Curent_User with a DSC resource<div class="MsoNormal">
I built a fun new resource for managing registry settings. “DSC
already has a resource for managing the registry” you say? This one sets values
to user registry settings for all users.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> KevMar_UserRegistry</span><span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";">
<span style="color: blueviolet;">DisableScreenSaver</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">ID</span> <span style="color: blueviolet;">=</span>
<span style="color: darkred;">"DisableScreenSaver"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">Key</span> <span style="color: blueviolet;">=</span> <span style="color: darkred;">"HKEY_CURRENT_USER\Control Panel\Desktop"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">ValueName</span>
<span style="color: blueviolet;">=</span> <span style="color: darkred;">"ScreenSaveActive"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">ValueData</span>
<span style="color: blueviolet;">=</span> <span style="color: darkred;">"0"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> } <o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
How cool is that? The built in DSC registry resource can
only manage system settings. For servers this is all you really need. But if
you have to manage user settings for some reason, forget about it. You need to
use my resource to do it.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
There are several limitations with my implementation to
understand before we dive into how it works. <o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
First
is that this setting applies to <b>all existing users and every new user</b> once it
is set. So if you remove this setting from future configurations instead of
using the <span style="color: blue; font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";">Ensure</span><span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";">
<span style="color: blueviolet;">=</span> <span style="color: darkred;">"Absent"
</span></span>option, new users to the system will continue to get the setting.
The good news is that using Ensure = “Absent” does stop this from applying to
new users.<o:p></o:p></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
Second
is that this <b>sets the value only once per user</b>. This kind of breaks the idea of
DSC maintaining configuration drift. If this needs to get reapplied, there is a
<b>version attribute</b> that must be used and incremented. Each user keeps track of
what version of the setting they have applied. Increasing the version signals
the user that something has changed and it needs to be set again. This is
important if you are changing the <span style="color: blue; font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";">ValueData</span> to something different.<o:p></o:p></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal">
Third these registry settings are only <b>applied at user
logon</b>. I am using a method that hooks into the user logon process to apply the
registry settings. I do not flag a reboot to DSC. I considered it but if you
are starting to manage user settings, there can be a huge number of these in
your configurations. Requiring a reboot for each one feels like a bit much. In
my use case, I did not want the reboot. This is why making it as Absent can
stop if from applying to any more users.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<br />
<div class="MsoNormal">
I’ll do a write up about how I did this in a future post. I
used a Windows feature that is not very well known to most systems admins. I
have it posted over at <a href="https://github.com/kmarquette/Powershell/tree/master/DSCModules/KevMar/DSCResources">https://github.com/kmarquette/Powershell/tree/master/DSCModules/KevMar/DSCResources</a>
if you want to check it out.<o:p></o:p></div>
Kevin Marquettehttp://www.blogger.com/profile/16389030891301082770noreply@blogger.com0tag:blogger.com,1999:blog-8683693.post-88122762813501226992014-11-24T20:03:00.000-06:002014-11-24T20:03:26.502-06:00Use Show-Command for a quick PowerShell GUI <div class="MsoNormal">
We all love PowerShell and know how awesome it is. But not
everyone we work with is as willing to drop to the shell as we are. The good
news is there is a very easy way to give them the GUI they think they need.
Write your advanced function like you already do and have them run it with
Show-Command.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<table cellpadding="0" cellspacing="0" class="tr-caption-container" style="float: right; margin-left: 1em; text-align: right;"><tbody>
<tr><td style="text-align: center;"><a href="http://3.bp.blogspot.com/-Ey7LA8plnU0/VHPi6kCiZUI/AAAAAAAAAUA/m-qogaBjfKw/s1600/Show-command.jpg" imageanchor="1" style="clear: right; margin-bottom: 1em; margin-left: auto; margin-right: auto;"><img border="0" src="http://3.bp.blogspot.com/-Ey7LA8plnU0/VHPi6kCiZUI/AAAAAAAAAUA/m-qogaBjfKw/s1600/Show-command.jpg" height="400" width="372" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Show-Command Set-Service</td></tr>
</tbody></table>
<div class="MsoNormal">
This beautiful little command pops up and asks them for the
information needed to run the command. Give it a try with <b>Show-Command
Set-Service</b>. </div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Not only are the required values marked, you even get a drop down
box for some options. This works if you target *.ps1 files too.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<br />
<div class="MsoNormal">
As cool as this is, <b>Show-Command</b> gets better. Run it on its
own and it gives you a list of all commands on the system. The filter makes it
very easy to find what you are looking for. <o:p></o:p></div>
Kevin Marquettehttp://www.blogger.com/profile/16389030891301082770noreply@blogger.com0tag:blogger.com,1999:blog-8683693.post-70554694366554095762014-11-20T20:33:00.000-06:002015-11-04T10:01:31.152-06:00$Using: scope in DSC script resourcesIf you have spent any time working with DSC resources, you have found yourself needing to use the Script resource for some things.<br />
<br />
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";">Script
<span style="color: blueviolet;">Script_Resource</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";">{<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> GetScript <span style="color: darkgrey;">=</span>
{}<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> SetScript <span style="color: darkgrey;">=</span>
{}<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> TestScript <span style="color: darkgrey;">=</span>
{}<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> DependsOn <span style="color: darkgrey;">=</span>
<span style="color: darkred;">""</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> Credential <span style="color: darkgrey;">=</span>
<span style="color: darkred;">""</span></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";">}</span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"><br /></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
It is easy enough to use. Define your Get, Set, and Test PowerShell commands and you are all set. Everything is good until you need to work with passing in variables into your script block. You will quickly find that it does not work like you would expect.<br />
<br />
This command will always return false:</div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "lucida console";"><br /></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: orangered; font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";">$path</span><span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: darkgrey;">=</span> <span style="color: darkred;">"c:\windows\temp"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";">Script
<span style="color: blueviolet;">Script_Resource</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";">{<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> TestScript <span style="color: darkgrey;">=</span>
{<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">Test-Path</span>
<span style="color: orangered;">$path</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";">}
<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Because $path is not defined within the scope of the TestScript to a value, the Test-Path will return false. Take a look at the mof file and you can see this. </div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: blueviolet; font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";">instance</span><span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">of</span> <span style="color: blueviolet;">MSFT_ScriptResource</span>
<span style="color: blueviolet;">as</span> <span style="color: orangered;">$MSFT_ScriptResource1ref</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";">{<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">ResourceID</span> <span style="color: blueviolet;">=</span> <span style="color: darkred;">"[Script]Script_Resource"</span>;</span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">TestScript</span> <span style="color: blueviolet;">=</span> <span style="color: darkred;">"\n Test-Path
'</span><span style="color: orangered;">$path</span><span style="color: darkred;">'\n
"</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">SourceInfo</span> <span style="color: blueviolet;">=</span> <span style="color: darkred;">"::7::9::Script"</span>;</span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">ModuleName</span> <span style="color: blueviolet;">=</span> <span style="color: darkred;">"PSDesiredStateConfiguration"</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">ModuleVersion</span> <span style="color: blueviolet;">=</span> <span style="color: darkred;">"1.0"</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";">};
<o:p></o:p></span></div>
<div class="MsoNormal">
</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
I have found two ways to deal with this issue. If you think about it, the TestScript is just a string that gets ran on the target node. If you look at the resource, TestScript is defined as a String.</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: orangered; font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";">$path</span><span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: darkgrey;">=</span> <span style="color: darkred;">"c:\windows\temp"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";">Script
<span style="color: blueviolet;">Script_Resource</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";">{<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> TestScript <span style="color: darkgrey;">=</span><span style="color: darkred;">"Test-Path '</span><span style="color: orangered;">$path</span><span style="color: darkred;">'"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";">}
<o:p></o:p></span></div>
<div class="MsoNormal">
</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
This works really well when the command is a very simple one line script. Take a look at the mof file now.</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: blueviolet; font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";">instance</span><span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">of</span> <span style="color: blueviolet;">MSFT_ScriptResource</span>
<span style="color: blueviolet;">as</span> <span style="color: orangered;">$MSFT_ScriptResource1ref</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";">{<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">ResourceID</span> <span style="color: blueviolet;">=</span> <span style="color: darkred;">"[Script]Script_Resource"</span>;</span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">TestScript</span> <span style="color: blueviolet;">=</span> <span style="color: darkred;">"Test-Path
'c:\\windows\\temp'"</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">SourceInfo</span> <span style="color: blueviolet;">=</span> <span style="color: darkred;">"::6::9::Script"</span>;</span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">ModuleName</span> <span style="color: blueviolet;">=</span> <span style="color: darkred;">"PSDesiredStateConfiguration"</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">ModuleVersion</span> <span style="color: blueviolet;">=</span> <span style="color: darkred;">"1.0"</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal">
</div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";">}; <o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"><br /></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
This could end up very messy if the script block gets more complicated. What if you have variables that you want to define in the script and use some from the parent scope. You end up escaping things with that horrible back tick. But there is a better way.</div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
This is where the $using: scope comes to the rescue. As far as I can tell, this is undocumented for use in script resources. But using it in Invoke-Command script blocks will allow you to reference variables in the parent scope. It works for our script resource too.</div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: orangered; font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";">$path</span><span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: darkgrey;">=</span> <span style="color: darkred;">"c:\windows\temp"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";">Script
<span style="color: blueviolet;">Script_Resource</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";">{<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> TestScript <span style="color: darkgrey;">=</span>
{<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">Test-Path</span>
<span style="color: orangered;">$using:path</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
</div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";">} <o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
Now when we dive into the mof file, we can see just how this magic works. Our $path gets inserted into the script as part of the script. </div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";">instance</span><span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blueviolet;">of</span>
<span style="color: blueviolet;">MSFT_ScriptResource</span> <span style="color: blueviolet;">as</span> <span style="color: orangered;">$MSFT_ScriptResource1ref</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";">{<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">ResourceID</span> <span style="color: blueviolet;">=</span> <span style="color: darkred;">"[Script]Script_Resource"</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">TestScript</span> <span style="color: blueviolet;">=</span> <span style="color: darkred;">"</span><span style="color: orangered;">$path</span><span style="color: darkred;">='c:\\windows\\temp'\n
Test-Path </span><span style="color: orangered;">$path</span><span style="color: darkred;">\n "</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">SourceInfo</span> <span style="color: blueviolet;">=</span> <span style="color: darkred;">"::7::9::Script"</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">ModuleName</span> <span style="color: blueviolet;">=</span> <span style="color: darkred;">"PSDesiredStateConfiguration"</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">ModuleVersion</span> <span style="color: blueviolet;">=</span> <span style="color: darkred;">"1.0"</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "lucida console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";">};
<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
The $using: scope is something I often overlook but this will be a very handy way to use it.<br />
<br />
One final note about my examples. I did trim them down to minimize the code. If you want to recreate my tests, you will need to have the SetScript and GetScript properties defined for each script block.</div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<br /></div>
Kevin Marquettehttp://www.blogger.com/profile/16389030891301082770noreply@blogger.com0tag:blogger.com,1999:blog-8683693.post-40270364647044779662014-11-03T20:23:00.000-06:002014-12-02T00:24:53.122-06:00Using Pester to validate DSC resources and configurationsThe more I use Pester, the more I like it. I found some ways to leverage it in validating my use of DSC Resources and configurations. Here are some samples to give you an idea of what I am talking about.<br />
<br />
<b>Are my resources loaded?</b><br />
Often I will create a new resource thinking it will work only to not have it loaded for some reason. At the moment all my resources are in the same module. So I have this test to use my folder structure to test for a loaded resource.<br />
<br />
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: orangered; font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";">$LoadedResources</span><span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: darkgrey;">=</span> <span style="color: blue;">Get-DscResource</span>
<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";">Describe</span><span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: darkred;">“DSCResources
located in </span><span style="color: orangered;">$PSScriptRoot</span><span style="color: darkred;">\DSCResources"</span> {<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: orangered;">$ResourceList</span>
<span style="color: darkgrey;">=</span> <span style="color: blue;">Get-ChildItem</span>
<span style="color: darkred;">"</span><span style="color: orangered;">$PSScriptRoot</span><span style="color: darkred;">\DSCResources"</span> <o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: darkblue;">Foreach</span>(<span style="color: orangered;">$Resource</span> <span style="color: darkblue;">in</span>
<span style="color: orangered;">$ResourceList</span>){<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">It</span> <span style="color: darkred;">"</span><span style="color: orangered;">$Resource</span><span style="color: darkred;"> Is Loaded [Dynamic]"</span> {<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: orangered;">$LoadedResources</span>
<span style="color: darkgrey;">|</span> <o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">Where-Object</span>{<span style="color: orangered;">$_</span><span style="color: darkgrey;">.</span>name <span style="color: darkgrey;">-eq</span> <span style="color: orangered;">$Resource</span>}
<span style="color: darkgrey;">|</span> <o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">Should</span>
<span style="color: blueviolet;">Not</span> <span style="color: blueviolet;">BeNullOrEmpty</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";">}
<o:p></o:p></span></div>
<br />
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Can my resource generate a mof file?</b></div>
<div class="MsoNormal">
The next thing I want to know is if it will generate a mof file. I create a test like this for every DSC resource. I use the TestDrive: location to manage temporary files.</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";">Describe</span><span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: darkred;">"Firewall"</span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">It</span> <span style="color: darkred;">"Creates a mof file"</span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: darkblue;">configuration</span>
<span style="color: blueviolet;">DSCTest</span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">Import-DscResource</span>
<span style="color: navy;">-modulename</span> <span style="color: blueviolet;">MyModule</span> <o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">Node</span>
<span style="color: blueviolet;">Localhost</span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">Firewall</span>
<span style="color: blueviolet;">SampleConfig</span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">State</span>
<span style="color: blueviolet;">=</span> <span style="color: darkred;">"ON"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">DSCTest</span>
<span style="color: navy;">-OutputPath</span> <span style="color: blueviolet;">Testdrive:\dsc</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: darkred;">"TestDrive:\dsc\localhost.mof"</span>
<span style="color: darkgrey;">|</span> <span style="color: blue;">Should</span> <span style="color: blueviolet;">Exist</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> }<o:p></o:p></span></div>
<div class="MsoNormal">
</div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";">} <o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Do my Test and Get functions work?</b></div>
<div class="MsoNormal">
For some of my resources, I even test the Get and Test functions inside the module. I first have to rename the script so I can load it. Also notice I use a Mock function to keep the Export-Module from throwing errors.</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: orangered; font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";">$here</span><span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: darkgrey;">=</span> <span style="color: blue;">Split-Path</span> <span style="color: navy;">-Parent</span> <span style="color: orangered;">$MyInvocation</span><span style="color: darkgrey;">.</span>MyCommand<span style="color: darkgrey;">.</span>Path<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";">Describe</span><span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: darkred;">"Firewall"</span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">Copy-Item</span> <span style="color: darkred;">"</span><span style="color: orangered;">$here</span><span style="color: darkred;">\Firewall.psm1"</span> <span style="color: blueviolet;">TestDrive:\script.ps1</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">Mock</span> <span style="color: blueviolet;">Export-ModuleMember</span> {<span style="color: darkblue;">return</span>
<span style="color: orangered;">$true</span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: darkgrey;">.</span> <span style="color: darkred;">"TestDrive:\script.ps1"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">It</span> <span style="color: darkred;">"Test-TargetResource returns true or false"</span>
{<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">Test-TargetResource</span>
<span style="color: navy;">-state</span> <span style="color: darkred;">"ON"</span>
<span style="color: darkgrey;">|</span> <o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">Should</span>
<span style="color: blueviolet;">Not</span> <span style="color: blueviolet;">BeNullOrEmpty</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">It</span> <span style="color: darkred;">"Get-TargetResource returns State = on or off"</span>
{<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> (<span style="color: blue;">Get-TargetResource</span>
<span style="color: navy;">-state</span> <span style="color: darkred;">"ON"</span>)<span style="color: darkgrey;">.</span>state <span style="color: darkgrey;">|</span> <o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> <span style="color: blue;">Should</span>
<span style="color: blueviolet;">Match</span> <span style="color: darkred;">"on|off"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-bottom: 0.0001pt;">
<span style="font-family: "Lucida Console"; font-size: 12.0pt; mso-bidi-font-family: "Lucida Console";">}
<o:p></o:p></span></div>
<div class="MsoNormal">
</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Edit: I added a <a href="http://kevinmarquette.blogspot.com/2014/12/using-pester-to-validate-dsc-resources.html" target="_blank">part 2</a>.</div>
Kevin Marquettehttp://www.blogger.com/profile/16389030891301082770noreply@blogger.com0tag:blogger.com,1999:blog-8683693.post-45216931182292004202014-11-03T19:59:00.003-06:002014-11-03T19:59:46.419-06:00Reading the Windows PowerShell Language Specification Version 3.0Anytime I spend a good deal of time with a technology, I eventually track down the core documentation. The real technical stuff that shows you everything. I just rediscovered the <a href="http://www.microsoft.com/en-us/download/details.aspx?id=36389" target="_blank">Windows PowerShell Language Specification Version 3.0</a>. It would be nice to have one for PowerShell 4.0, but I did not see it yet. I rediscovered a few things that I either missed before or I was not ready to understand them yet. Here are some hidden gems that I would like to share with you.<br />
<br />
<b>Wildcard attributes</b><br />
We use these all the time for partial matches. The cool thing is that we can use them in file paths to skip over folders that may have any name. Let me show you how we can use this to fix an old problem of mine.<br />
<br />
Our home folders are also the users my documents folder. Every time a user logs in, they create a desktop.ini file in that location. It does this cool trick of renaming the folder to say "My Documents". This is nice, unless you are an admin looking a list of 500 folders called "My Documents". Here is a quick fix to delete all of those files.<br />
<br />
Get-ChildItem d:\profile\*\desktop.ini | Remove-Item -Force<br />
<br />
<b>ForEach-Object -Member</b><br />
I can't say that I have many uses for this one, but I think it is interesting. The ForEach-Object has a -MemberName property. If you provide it an objects function name, it will call it for each object. Here is one good example of using it to uninstall software.<br />
<br />
Get-WMIObject Win32_Product | Where-Object name -match Java | ForEach-Object -MemberName Uninstall<br />
<br />
Or we can shorthand this same command even more like this:<br />
<br />
gwmi win32_product | ? name -match Java | % uninstall<br />
<br />
<span class="Codefragment"><span style="font-size: 11.0pt; mso-ansi-language: EN-US; mso-bidi-font-family: "Times New Roman"; mso-bidi-font-size: 10.0pt; mso-bidi-language: AR-SA; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-US;"><b>$PsBoundParameters</b></span></span><br />
<span class="Codefragment"><span style="font-size: 11.0pt; mso-ansi-language: EN-US; mso-bidi-font-family: "Times New Roman"; mso-bidi-font-size: 10.0pt; mso-bidi-language: AR-SA; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-US;">This variable contains a dictionary of all the parameters that were passed to the function or script. The cool thing is that you can splat these to another function. This would be great when you are creating a wrapper around a function that takes lots of parameters. I don;t have a good example off hand, but I know I have code out there that would be much cleaner with this</span></span><br />
<br />
<br />Kevin Marquettehttp://www.blogger.com/profile/16389030891301082770noreply@blogger.com0tag:blogger.com,1999:blog-8683693.post-79298718862465826442014-07-24T10:28:00.002-05:002014-07-24T10:29:18.070-05:00Loading .Net Assemblys into PowershellI have some functionality written in C# that I want to use in PowerShell. I compiled the code I wanted into a dll and copied it to my target system. Then I used this snip of code to load and use it:<br />
<div>
<br /></div>
<div>
<pre>[System.Reflection.Assembly]::LoadFrom(".\myProject.dll")
$myObject = New-Object myNamespace.myClass
$myObject.MyFunction()
</pre>
<br />
If my functions return objects, I can use them just like I would any other object in PowerShell. I just found this as another way to use the .Net Framework.</div>
Kevin Marquettehttp://www.blogger.com/profile/16389030891301082770noreply@blogger.com0tag:blogger.com,1999:blog-8683693.post-90138041178430113032014-07-13T18:21:00.001-05:002014-07-13T18:21:34.749-05:00The compounding power of automationI was recently reviewing some of my past automation and development projects. I took the time to calculate the man hours my projects saved the organization. Over the last 9 years it has added up to some substantial savings. I have directly saved 44,000+ man hours. Because those tasks were automated, the frequency of that work was increased. I estimate that over a 5 year window, my projects are doing the work of 215,000+ man hours.<br />
<br />
I want to take a moment to point out this <a href="http://xkcd.com/">xkcd.com</a> image below. I used the 5 year metric because of this chart.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://imgs.xkcd.com/comics/is_it_worth_the_time.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://imgs.xkcd.com/comics/is_it_worth_the_time.png" height="521" width="640" /></a></div>
<br />
I think every system administrator automates things all the time without thinking about it. I included several of those in my calculation.<br />
<br />
Every week, we would make a copy of the production data onto a second server for reports. It would take me about an hour to create a one off backup, restore it to a second server, and run some post processing scripts. If I spent an hour each week over the last 9 years, it would have taken 468 hours of my time. No admin in their right mind is doing something like this by hand. I automated it and did something else more productive with those 468 hours.<br />
<br />
The advantage of automating it was running it more often to give the business better access to the data. I made it a daily process and automated what would have been 2,340 man hours of time to do the same thing.<br />
<br />
I have one project where I saved 4 seconds (80% improvement) off of 1.1 million actions. One automation script took my department out of the account provisioning process saving 270 hours over 3 years. I have another one that took someone 1 week to generate a set of report 4 times a year and I made the whole set process daily. There are 20+ projects where I saved the company time and made it more productive.<br />
<br />
These savings are not imaginary. There are a few cases where staff resources were reassigned to other areas because of this automation. Part of the reason I got involved in many of these projects is because they took too much time and there had to be a better way. I am good at finding that better way.Kevin Marquettehttp://www.blogger.com/profile/16389030891301082770noreply@blogger.com0