<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-4687813131860472363</id><updated>2024-09-08T07:58:01.050-07:00</updated><category term="array"/><category term="credentials"/><category term="in32_QuickFixEngineering"/><category term="powershell"/><category term="wmi"/><category term="wql"/><title type='text'>Powershell Masters</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://powershellmasters.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4687813131860472363/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://powershellmasters.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>5</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-4687813131860472363.post-6338172603688280955</id><published>2009-04-10T23:27:00.000-07:00</published><updated>2009-04-10T23:32:50.985-07:00</updated><title type='text'>Powershell and remote WMI autenthication</title><content type='html'>&lt;div style=&quot;text-align: justify;&quot;&gt;In version 1.0 of Windows Power­Shell, gmwi (which is an Alias for Get-WmiObject) is about the only cmdlet that directly supports remote management. This is due mainly to the fact that remote control is built into the underlying Windows Management Instrumentation architecture. And, because Windows PowerShell is simply utilizing that already existing architecture, it’s subject to that architecture’s security features.&lt;br /&gt;&lt;br /&gt;Beeing able to perform remote WMI queries is particularly useful when doing a computer inventory for big companies having different domains and forests.&lt;br /&gt;&lt;br /&gt;Here’s an example of standard gwmi query:&lt;br /&gt;&lt;br /&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;gwmi -namespace “root\cimv2” -computer $Comp -list&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;Get-WmiObject : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) At line:1 char:5&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;In this instance, I tried to connect to a remote computer called $Comp that I don’t have permission to access. I might, for example, be logged on in a different, untrusted active directory domain, or I could be logged on with a less-privileged account.*&lt;br /&gt;&lt;br /&gt;Fortunately, gwmi supports a –credential parameter that allows me to specify an alternate set of user credentials for my WMI connection.&lt;br /&gt;&lt;br /&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;gwmi win32_service –credential mydomain\administrator –computer SComp&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;My username—is provided in the DOMAIN\Username format.&lt;br /&gt;&lt;br /&gt;Note that there’s nowhere you can enter a password. Windows PowerShell will prompt me for that. Windows PowerShell deliberately doesn’t provide a way to enter a password on the command line because doing so would enable you to hardcode passwords into script files, which is an absolute security risk.&lt;br /&gt;&lt;br /&gt;However, there are other ways you can work with this –credential parameter&lt;br /&gt;&lt;br /&gt;The first one is by creating a sort of credential object, called a PSCredential, ahead of time.&lt;br /&gt;&lt;br /&gt;The key is the Get-Credential cmdlet:&lt;br /&gt;&lt;br /&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;$cred = get-credential mydomain\administrator&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;When I run this, I’m still prompted for the matching password. This time, however, the credential object that is created is stored in the $cred variable. If I look at the contents of $cred, I’ll see the name but not the password:&lt;br /&gt;&lt;br /&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;$cred&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;UserName&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;--------&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;mydomain\administrator&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;I can then reuse that credential object as much as I like:&lt;br /&gt;&lt;br /&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;gwmi win32_service –credential $cred –computer $Comp&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;This simplifies repeated WMI connections to a remote computer by predefining a reusable credential object. But I still have to type my password once at the beginning of the script, which is not good if we&#39;d want to schedule the script execution...&lt;br /&gt;&lt;br /&gt;The best solution is to previously store the password in a secure file on your disk. Once you store you password, you won&#39;t have to enter your credentials over and over again, instead you can just read in your password from the file directly from the script, and create a new PSCredential object from that. Then you can use that credential to perform various unattended administration tasks:&lt;br /&gt;&lt;br /&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;$user = &quot;mydomain\administrator&quot;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;$pass = cat securepass.txt | convertto-securestring                                                            &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $user,$pass&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;For more information on how to store your password in a file, have a look at &lt;a href=&quot;http://powershellmasters.blogspot.com/2009/04/store-secure-password-in-file-with.html&quot;&gt;this post&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Please note (and this is very important) that &lt;span style=&quot;font-weight: bold;&quot;&gt;only the person that created the password file can decrypt it&lt;/span&gt;. Try it out, log on with different credentials and try getting the password. Thus, even if they have access to the password file, unless they have your credentials, they won&#39;t be able to get the password.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://powershellmasters.blogspot.com/feeds/6338172603688280955/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://powershellmasters.blogspot.com/2009/04/powershell-and-remote-wmi.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4687813131860472363/posts/default/6338172603688280955'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4687813131860472363/posts/default/6338172603688280955'/><link rel='alternate' type='text/html' href='http://powershellmasters.blogspot.com/2009/04/powershell-and-remote-wmi.html' title='Powershell and remote WMI autenthication'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4687813131860472363.post-4072261214590238145</id><published>2009-04-08T07:21:00.000-07:00</published><updated>2009-04-08T07:21:26.738-07:00</updated><title type='text'>Store a secure password in a file with Powershell</title><content type='html'>## ===================&lt;br&gt;## This is a one-liner to ask for a password a store it safely in a&lt;br&gt;securepass.txt file&lt;br&gt;read-host -prompt &amp;quot;Enter password to be encrypted in securepass.txt &amp;quot;&lt;br&gt;-assecurestring | convertfrom-securestring | out-file securepass.txt&lt;br&gt;## ===================</content><link rel='replies' type='application/atom+xml' href='http://powershellmasters.blogspot.com/feeds/4072261214590238145/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://powershellmasters.blogspot.com/2009/04/store-secure-password-in-file-with.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4687813131860472363/posts/default/4072261214590238145'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4687813131860472363/posts/default/4072261214590238145'/><link rel='alternate' type='text/html' href='http://powershellmasters.blogspot.com/2009/04/store-secure-password-in-file-with.html' title='Store a secure password in a file with Powershell'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4687813131860472363.post-6487621726485031524</id><published>2009-04-08T00:53:00.000-07:00</published><updated>2009-04-08T00:53:28.654-07:00</updated><title type='text'>NSLOOKUP and POWERSHELL</title><content type='html'># FORWARD DNS RESOLUTON WITH NSLOOKUP&lt;br /&gt;# This function calls nslookup (which is a standard microsoft tool)&lt;br /&gt;# and fetches from the output string just the IP address.&lt;br /&gt;# You can specify a Name Server to use.&lt;br /&gt;# Please note the $Comp can be an array, in which case you should&lt;br /&gt;# run into it with foreach().&lt;br /&gt;# ==========================================&lt;br /&gt;Function forward_dns&lt;br /&gt; {&lt;br /&gt; $cmd = &quot;nslookup &quot; + $args[0] + &quot; &quot; + $ns&lt;br /&gt; $result = Invoke-Expression ($cmd)&lt;br /&gt; trap&lt;br /&gt;  {&lt;br /&gt;  $global:controlladns = $true&lt;br /&gt;  $global:solved_ip = &quot;No record found&quot;&lt;br /&gt;  continue&lt;br /&gt;  }&lt;br /&gt;  $global:controlladns = $false&lt;br /&gt;  $global:solved_ip = $result.SyncRoot[4]&lt;br /&gt;  if (-not $global:controlladns)&lt;br /&gt;   {&lt;br /&gt;   $leng = $global:solved_ip.Length -10&lt;br /&gt;   $global:solved_ip =&lt;br /&gt;$global:solved_ip.substring(10,$leng)&lt;br /&gt;   }&lt;br /&gt; }&lt;br /&gt;$comp = &quot;hostname&quot; # Hostname to resolve to IP&lt;br /&gt;$ns = &quot;DNS name&quot; # Name Server which will do name resolution&lt;br /&gt;forward_dns $comp&lt;br /&gt;echo &quot;Host: $comp - IP : $global:solved_ip - NS: $ns&quot;&lt;br /&gt;# ==========================================&lt;p&gt;&lt;br /&gt;# REVERSE DNS RESOLUTON WITH NSLOOKUP&lt;br /&gt;# This function calls nslookup (which is a standard microsoft tool)&lt;br /&gt;# and fetches from the output string just the hostname.&lt;br /&gt;# You can specify a Name Server to use.&lt;br /&gt;# Please note the $Comp can be an array, in which case you should&lt;br /&gt;# run into it with foreach().&lt;br /&gt;# ==========================================&lt;br /&gt;Function reverse_dns&lt;br /&gt; {&lt;br /&gt; $cmd2 = &quot;nslookup &quot; + $args[0] + &quot; &quot; + $ns&lt;br /&gt; $result2 = Invoke-Expression ($cmd2)&lt;br /&gt; $global:reverse_solved_ip =  $result2.SyncRoot[3]&lt;br /&gt; if ($result2.count -lt 4) # Integrity check&lt;br /&gt;  {&lt;br /&gt;  $global:reverse_solved_ip = &quot;No record found&quot;&lt;br /&gt;  }&lt;br /&gt; else&lt;br /&gt;  {&lt;br /&gt;  $leng2 = $global:reverse_solved_ip.length - 9&lt;br /&gt;  $global:reverse_solved_ip =&lt;br /&gt;$global:reverse_solved_ip.substring(9,$leng2)&lt;br /&gt;  }&lt;br /&gt; }&lt;br /&gt;$comp = &quot;X.X.X.X&quot; # Hostname to resolve to IP&lt;br /&gt;$ns = &quot;DNS name&quot; # Name Server which will do name resolution&lt;br /&gt;reverse_dns $comp&lt;br /&gt;echo &quot;IP: $comp - Host : $global:reverse_solved_ip - NS: $ns&quot;&lt;br /&gt;# ==========================================&lt;/p&gt;&lt;p&gt;# ==========================================&lt;br /&gt;# Don&#39;t use DIG or get-dns.&lt;br /&gt;# You don&#39;t need to install any add-on just for name resolution.&lt;br /&gt;# Remember to open PORT 53 on you firewall for DNS requests.&lt;br /&gt;# ==========================================&lt;/p&gt;&lt;p&gt;# ==========================================&lt;br /&gt;# Note also the use of the &quot;.substring&quot; method.&lt;br /&gt;# Example:&lt;br /&gt;# $a=&quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;&lt;br /&gt;# $a = $a.substring(0,3)&lt;br /&gt;# When you run this command and then echo back the value of $a you should&lt;br /&gt;get the following:&lt;br /&gt;# ABC&lt;br /&gt;# ==========================================&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://powershellmasters.blogspot.com/feeds/6487621726485031524/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://powershellmasters.blogspot.com/2009/04/nslookup-and-powershell.html#comment-form' title='9 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4687813131860472363/posts/default/6487621726485031524'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4687813131860472363/posts/default/6487621726485031524'/><link rel='alternate' type='text/html' href='http://powershellmasters.blogspot.com/2009/04/nslookup-and-powershell.html' title='NSLOOKUP and POWERSHELL'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>9</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4687813131860472363.post-5335365846464692733</id><published>2009-04-08T00:30:00.001-07:00</published><updated>2009-04-08T00:30:40.672-07:00</updated><title type='text'>Powershell Operators</title><content type='html'>-eq             Equal (case insensitive)&lt;br /&gt;-ne             Not equal (case insensitive)&lt;br /&gt;-ge             Greater than or equal (case insensitive)&lt;br /&gt;-gt             Greater than (case insensitive)&lt;br /&gt;-lt             Less than (case insensitive)&lt;br /&gt;-le             Less than or equal (case insensitive)&lt;br /&gt;-like           Wildcard comparison (case insensitive)&lt;br /&gt;-notlike        Wildcard comparison (case insensitive)&lt;br /&gt;-match          Regular expression comparison (case insensitive)&lt;br /&gt;-notmatch       Regular expression comparison (case insensitive)&lt;br /&gt;-replace        Replace operator (case insensitive)&lt;br /&gt;-contains       Containment operator (case insensitive)&lt;br /&gt;-notcontains    Containment operator (case insensitive)&lt;br /&gt;## ====================================&lt;br /&gt;-ieq            Case insensitive equal&lt;br /&gt;-ine            Case insensitive not equal&lt;br /&gt;-ige            Case insensitive greater than or equal&lt;br /&gt;-igt            Case insensitive greater than&lt;br /&gt;-ile            Case insensitive less than or equal&lt;br /&gt;-ilt            Case insensitive less than&lt;br /&gt;-ilike          Case insensitive equal&lt;br /&gt;-inotlike       Case insensitive equal&lt;br /&gt;-imatch         Case insensitive regular expression comparison&lt;br /&gt;-inotmatch      Case insensitive regular expression comparison&lt;br /&gt;-ireplace       Case insensitive replace operator&lt;br /&gt;-icontains      Case insensitive containment operator&lt;br /&gt;-inotcontains   Case insensitive containment operator&lt;br /&gt;## ====================================&lt;br /&gt;-ceq             Equal (case sensitive)&lt;br /&gt;-cne             Not equal (case sensitive)&lt;br /&gt;-cge             Greater than or equal (case sensitive)&lt;br /&gt;-cgt             Greater than (case sensitive)&lt;br /&gt;-clt             Less than (case sensitive)&lt;br /&gt;-cle             Less than or equal (case sensitive)&lt;br /&gt;-clike           Wildcard comparison (case sensitive)&lt;br /&gt;-cnotlike        Wildcard comparison (case sensitive)&lt;br /&gt;-cmatch          Regular expression comparison (case sensitive)&lt;br /&gt;-cnotmatch       Regular expression comparison (case sensitive)&lt;br /&gt;-creplace        Replace operator (case sensitive)&lt;br /&gt;-ccontains       Containment operator (case sensitive)&lt;br /&gt;-cnotcontains    Containment operator (case sensitive)&lt;br /&gt;## ====================================&lt;br /&gt;-is             Is of a type&lt;br /&gt;-isnot          Is not of a type&lt;br /&gt;-as             As a type, no error if conversion fails</content><link rel='replies' type='application/atom+xml' href='http://powershellmasters.blogspot.com/feeds/5335365846464692733/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://powershellmasters.blogspot.com/2009/04/powershell-operators.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4687813131860472363/posts/default/5335365846464692733'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4687813131860472363/posts/default/5335365846464692733'/><link rel='alternate' type='text/html' href='http://powershellmasters.blogspot.com/2009/04/powershell-operators.html' title='Powershell Operators'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4687813131860472363.post-3937611397352489931</id><published>2009-04-07T08:01:00.000-07:00</published><updated>2009-04-08T00:31:14.936-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="array"/><category scheme="http://www.blogger.com/atom/ns#" term="credentials"/><category scheme="http://www.blogger.com/atom/ns#" term="in32_QuickFixEngineering"/><category scheme="http://www.blogger.com/atom/ns#" term="powershell"/><category scheme="http://www.blogger.com/atom/ns#" term="wmi"/><category scheme="http://www.blogger.com/atom/ns#" term="wql"/><title type='text'>Powershell function to check last patches</title><content type='html'>This function uses WMI to connect to a remote host and retrieve the three last patches and their installation date. It uses the WQL (WMI query language) to fetch just the last three installed patches and not all of them.&lt;br /&gt;&lt;br /&gt;Prerequisites:&lt;br /&gt;&lt;br /&gt;You need an array named $computerlist containing a list of computers, i.e.&lt;br /&gt;$computers = &quot;host01&quot;,&quot;host02&quot;,&quot;host03&quot;&lt;br /&gt;&lt;br /&gt;And you need your credentials stored in a secure $creds variable.&lt;br /&gt;&lt;br /&gt;You could have used the registry, but in Powershell 1.0 you must une a .NET class which doesn&#39;t support remote autenthication in a different security context (i.e. a different AD domain).&lt;br /&gt;&lt;br /&gt;Please note that the Win32_QuickFixEngineering WMI class has a know bug and not all the pathces are listed... this is a very old problem.&lt;br /&gt;&lt;br /&gt;This is the code:&lt;br /&gt;&lt;br /&gt;#*=============================================================================&lt;br /&gt;#* Function: QFE&lt;br /&gt;#* Arguments: $Comp&lt;br /&gt;#* Output: $patchid01/02/03 and $patchdate01/02/03&lt;br /&gt;#* Purpose: Retrieve the 3 last patches&lt;br /&gt;#*&lt;br /&gt;#*=============================================================================&lt;br /&gt;function QFE&lt;br /&gt;   {&lt;br /&gt;   $global:checkkb = gwmi Win32_QuickFixEngineering -ComputerName $args[0] -credential $creds | ? { $_.InstalledOn } | sort installedon | select -Last 3 #| ft hotfixid, installedon&lt;br /&gt;   #$global:checkkb&lt;br /&gt;   $global:patchid01 = $global:checkkb.syncroot[0].hotfixid&lt;br /&gt;   $global:patchdate01 = $global:checkkb.syncroot[0].installedon&lt;br /&gt;   $global:patchid02 = $global:checkkb.syncroot[1].hotfixid&lt;br /&gt;   $global:patchdate02 = $global:checkkb.syncroot[1].installedon&lt;br /&gt;   $global:patchid03 = $global:checkkb.syncroot[2].hotfixid&lt;br /&gt;   $global:patchdate03 = $global:checkkb.syncroot[2].installedon&lt;br /&gt;       $global:patchid01 + &quot; &quot; + $global:patchdate01&lt;br /&gt;       $global:patchid02 + &quot; &quot; + $global:patchdate02&lt;br /&gt;       $global:patchid03 + &quot; &quot; + $global:patchdate03&lt;br /&gt;       $global:checkkb = $null&lt;br /&gt;   }&lt;br /&gt;foreach ($comp in $computerlist) {QFE}</content><link rel='replies' type='application/atom+xml' href='http://powershellmasters.blogspot.com/feeds/3937611397352489931/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://powershellmasters.blogspot.com/2009/04/powershell-function-to-check-last.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4687813131860472363/posts/default/3937611397352489931'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4687813131860472363/posts/default/3937611397352489931'/><link rel='alternate' type='text/html' href='http://powershellmasters.blogspot.com/2009/04/powershell-function-to-check-last.html' title='Powershell function to check last patches'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>