<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:media="http://search.yahoo.com/mrss/"><channel><title>powershell on Neolisk's Tech Blog</title><link>/tags/powershell/</link><description>Recent content in powershell on Neolisk's Tech Blog</description><generator>Hugo -- gohugo.io</generator><language>en</language><managingEditor>neolisk@gmail.com (Victor Zakharov)</managingEditor><webMaster>neolisk@gmail.com (Victor Zakharov)</webMaster><copyright>©2020-2024 Victor Zakharov. All Rights Reserved</copyright><lastBuildDate>Wed, 19 Apr 2017 00:00:00 +0000</lastBuildDate><atom:link href="/tags/powershell/index.xml" rel="self" type="application/rss+xml"/><item><title>Powershell - Get CPU Usage by Process (remote)</title><link>/posts/2017-04-19-powershell-get-cpu-usage-remote/</link><pubDate>Wed, 19 Apr 2017 00:00:00 +0000</pubDate><author>neolisk@gmail.com (Victor Zakharov)</author><atom:modified>Thu, 30 Jan 2020 19:42:30 -0500</atom:modified><guid>/posts/2017-04-19-powershell-get-cpu-usage-remote/</guid><description>You would expect Get-Process to do the job, but it turns out that its CPU metric is not on a 0..100% scale.
Here is a one-liner (find who&amp;rsquo;s eating into your CPU - or that of a remote server):
1 gwmi Win32_PerfFormattedData_PerfProc_Process -filter &amp;#34;Name &amp;lt;&amp;gt; &amp;#39;_Total&amp;#39; and Name &amp;lt;&amp;gt; &amp;#39;Idle&amp;#39;&amp;#34; -Computer $servername | where { $_.PercentProcessorTime -gt 0 } | select Name, PercentProcessorTime For some reason, the total percentage of all selected processes can be greater than 100, I&amp;rsquo;m guessing there was a measurement lag.</description><dc:creator>Victor Zakharov</dc:creator><category>powershell</category></item><item><title>Powershell - Export Logs from SVN</title><link>/posts/2013-04-17-powershell-export-svn-logs/</link><pubDate>Wed, 17 Apr 2013 00:00:00 +0000</pubDate><author>neolisk@gmail.com (Victor Zakharov)</author><atom:modified>Thu, 30 Jan 2020 20:47:42 -0500</atom:modified><guid>/posts/2013-04-17-powershell-export-svn-logs/</guid><description>You will need a command line SVN tool for this to work, for example, SlikSVN.
Install it on your machine and configure environment:path to point to its binaries.
Then use below Powershell script (I called it Get-SvnLog.ps1):
1 2 3 4 5 6 7 8 9 10 11 12 13 ([xml](svn log -v --xml)).log.logentry | % { $entry = $_; $_.paths.path | foreach { $obj = 1 | select -Property Revision,Author,Date,Message,Action,FilePath; $obj.</description><dc:creator>Victor Zakharov</dc:creator><category>powershell</category></item><item><title>Edit XML using Powershell</title><link>/posts/2012-09-24-powershell-edit-xml/</link><pubDate>Mon, 24 Sep 2012 00:00:00 +0000</pubDate><author>neolisk@gmail.com (Victor Zakharov)</author><atom:modified>Thu, 30 Jan 2020 20:47:42 -0500</atom:modified><guid>/posts/2012-09-24-powershell-edit-xml/</guid><description>Because there is no easy/built-in way to edit XML using Powershell, I wrote a script that can change parts of XML documents.
I used it for bulk edit of connections in Remote Desktop Manager, to change some values in RDP session over dozens of servers.
You can modify it for your needs - in simple cases you would only need to change ArrayOfConnection to your document&amp;rsquo;s root.
1 2 3 4 5 6 7 8 9 10 11 12 13 param($Path,$Key,$Value) $doc = [xml](gc $Path) $doc.</description><dc:creator>Victor Zakharov</dc:creator><category>powershell</category><category>xml</category></item><item><title>PowerShell - Convert Array To HashTable</title><link>/posts/2012-08-02-powershell-convert-array-to-hashtable/</link><pubDate>Thu, 02 Aug 2012 00:00:00 +0000</pubDate><author>neolisk@gmail.com (Victor Zakharov)</author><atom:modified>Thu, 30 Jan 2020 20:47:42 -0500</atom:modified><guid>/posts/2012-08-02-powershell-convert-array-to-hashtable/</guid><description>There are many ways to do this, depending on what you need. In this article I&amp;rsquo;m going to highlight 2 of them.
Function-style conversion:
1 2 3 4 5 6 function ArrayToHash($a) { $hash = @{} $a | foreach { $hash[$_.ProcessName] = $_ } return $hash } Usage:
1 ArrayToHash (Get-Process) Filter-style conversion:
1 2 3 4 5 6 filter ArrayToHash { begin { $hash = @{} } process { $hash[$_.</description><dc:creator>Victor Zakharov</dc:creator><category>powershell</category></item><item><title>PowerShell - Strip Html Tags From a String</title><link>/posts/2010-06-10-powershell-strip-html-tags-from-string/</link><pubDate>Thu, 10 Jun 2010 00:00:00 +0000</pubDate><author>neolisk@gmail.com (Victor Zakharov)</author><atom:modified>Sat, 10 Oct 2020 17:03:04 -0400</atom:modified><guid>/posts/2010-06-10-powershell-strip-html-tags-from-string/</guid><description>Here&amp;rsquo;s a one-liner:
1 $myStr -replace &amp;#34;&amp;lt;.*?&amp;gt;&amp;#34; Explanation With the above line, we are removing every shortest set of symbols within triangle brackets. The word shortest is very important here. Have a look at this wiki page for more information about greedy and lazy matching, under Lazy quantification.</description><dc:creator>Victor Zakharov</dc:creator><category>powershell</category><category>devops</category></item><item><title>PowerShell - Print All Odd/Even Lines</title><link>/posts/2010-03-30-powershell-print-all-odd-even-lines/</link><pubDate>Tue, 30 Mar 2010 00:00:00 +0000</pubDate><author>neolisk@gmail.com (Victor Zakharov)</author><atom:modified>Mon, 12 Oct 2020 10:25:43 -0400</atom:modified><guid>/posts/2010-03-30-powershell-print-all-odd-even-lines/</guid><description>As a little brainstormer, suppose you have a variable with the following test content:
1 2 3 4 5 6 7 8 $str = @&amp;#34; Line 1 Line 2 Line 3 Line 4 Line 5 Line 6 &amp;#34;@ Here is how you can print all odd lines from it:
1 $f=0; $str.Split(&amp;#34;`n&amp;#34;) | % { if($f = !$f) { $_ } } Or, if you want all even lines, change the initial $f value to 1:</description><dc:creator>Victor Zakharov</dc:creator><category>powershell</category><category>algorithm</category></item><item><title>PowerShell - Get MAC Address Of Any Remote IP</title><link>/posts/2010-02-09-powershell-get-mac-of-remote-ip/</link><pubDate>Tue, 09 Feb 2010 00:00:00 +0000</pubDate><author>neolisk@gmail.com (Victor Zakharov)</author><atom:modified>Mon, 12 Oct 2020 14:40:56 -0400</atom:modified><guid>/posts/2010-02-09-powershell-get-mac-of-remote-ip/</guid><description>Here is a PowerShell one-liner:
1 2 #change/assign $hostIp to a valid IP address (gwmi -Class Win32_NetworkAdapterConfiguration | where { $_.IpAddress -eq $hostIp }).MACAddress If you need to dynamically resolve a host IP from its name:
1 2 #change/assign $Computer to a valid host identifier $hostIp = [System.Net.Dns]::GetHostByName($Computer).AddressList[0].IpAddressToString The whole script may look like the following:
1 2 3 4 5 6 7 8 9 10 11 12 param ( $Computer , $Credential ) #to make it work without parameters if($Computer -eq $null) { $Computer = $env:COMPUTERNAME } #program logic $hostIp = [System.</description><dc:creator>Victor Zakharov</dc:creator><category>powershell</category><category>devops</category><category>algorithm</category></item><item><title>PowerShell - Control Code Execution</title><link>/posts/2010-02-03-powershell-control-code-execution/</link><pubDate>Wed, 03 Feb 2010 00:00:00 +0000</pubDate><author>neolisk@gmail.com (Victor Zakharov)</author><atom:modified>Mon, 12 Oct 2020 14:30:41 -0400</atom:modified><guid>/posts/2010-02-03-powershell-control-code-execution/</guid><description>In this article we are going to discuss basics of how to control code execution in a PowerShell script. There are several keywords, which have slightly different behavior: break, return and exit. To help you better understand what is going on, I will post all code blocks tagged with Powershell, immediately followed by its text output.
Break Just as in C++, you can use break to exit loops early:
1 2 3 4 5 6 7 8 #Sample 1.</description><dc:creator>Victor Zakharov</dc:creator><category>powershell</category><category>development</category></item><item><title>PowerShell - Get Current User</title><link>/posts/2010-02-03-powershell-get-current-user/</link><pubDate>Wed, 03 Feb 2010 00:00:00 +0000</pubDate><author>neolisk@gmail.com (Victor Zakharov)</author><atom:modified>Mon, 12 Oct 2020 14:33:05 -0400</atom:modified><guid>/posts/2010-02-03-powershell-get-current-user/</guid><description>Actually, there are many ways to get name of the user, which is currently logged on, including built-in .NET commands.
But let&amp;rsquo;s keep our code clean and stick with native PowerShell.
To get just the user name:
1 &amp;#34;$env:username&amp;#34; You might also need the domain:
1 &amp;#34;$env:userdomain&amp;#34; Or both of them:
1 &amp;#34;$env:userdomain\$env:username&amp;#34;</description><dc:creator>Victor Zakharov</dc:creator><category>powershell</category><category>devops</category></item><item><title>PowerShell - Check if remote port is open</title><link>/posts/2010-01-07-powershell-check-if-remote-port-open/</link><pubDate>Thu, 07 Jan 2010 00:00:00 +0000</pubDate><author>neolisk@gmail.com (Victor Zakharov)</author><atom:modified>Mon, 12 Oct 2020 16:17:47 -0400</atom:modified><guid>/posts/2010-01-07-powershell-check-if-remote-port-open/</guid><description>There are tons of ways to check if the port is open on a remote server. I think I found the fastest one, which is a Powershell one-liner:
1 (new-object Net.Sockets.TcpClient).Connect($host, $port) For testing purposes you can use the following command:
1 (new-object Net.Sockets.TcpClient).Connect(&amp;#34;google.com&amp;#34;, 80) It should give you no results. This means connection succeeded.
In real life you may encounter the following two outputs in red:
(1) Exception calling &amp;ldquo;Connect&amp;rdquo; with &amp;ldquo;2&amp;rdquo; argument(s): &amp;ldquo;A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond xxx.</description><dc:creator>Victor Zakharov</dc:creator><category>powershell</category><category>devops</category></item><item><title>PowerShell - Query Oracle Database</title><link>/posts/2009-10-06-powershell-query-oracle-database/</link><pubDate>Tue, 06 Oct 2009 00:00:00 +0000</pubDate><author>neolisk@gmail.com (Victor Zakharov)</author><atom:modified>Tue, 13 Oct 2020 15:08:19 -0400</atom:modified><guid>/posts/2009-10-06-powershell-query-oracle-database/</guid><description>You might have already googled other pages to help you on this. You even probably have found some PowerShell function like the one below:
1 2 3 4 5 6 7 8 9 function OracleQuery($Instance,$User,$Password,$Query) { $private:tmp = [System.Reflection.Assembly]::LoadWithPartialName(&amp;#34;System.Data.OracleClient&amp;#34;) $private:con = &amp;#34;Data Source=$instance;User Id=$user;Password=$password&amp;#34; $private:ad = New-Object System.Data.OracleClient.OracleDataAdapter($query,$con) $private:ds = New-Object System.Data.DataSet $private:tmp = $ad.Fill($ds) return $ds.Tables[0] } Still something makes this one different. Here are some tricks used here:</description><dc:creator>Victor Zakharov</dc:creator><category>powershell</category><category>oracle</category><category>devops</category></item><item><title>PowerShell - view current version</title><link>/posts/2009-08-28-powershell-view-current-version/</link><pubDate>Fri, 28 Aug 2009 00:00:00 +0000</pubDate><author>neolisk@gmail.com (Victor Zakharov)</author><atom:modified>Tue, 13 Oct 2020 15:19:59 -0400</atom:modified><guid>/posts/2009-08-28-powershell-view-current-version/</guid><description>Introduction I&amp;rsquo;m usually lazy to remember which version of PowerShell (if any) was installed on each and every server in our site. Because each generation of OS has its own way to install/reinstall PowerShell, sometimes I need to see if it&amp;rsquo;s anything there already and I need to perform a clean up.
Procedure Open PowerShell. Type the following: $host.version Press ENTER. You should get something like this:
v
Windows 7 Enterprise, PS 2.</description><dc:creator>Victor Zakharov</dc:creator><category>powershell</category></item><item><title>Powershell Special Characters And Tokens</title><link>/posts/2009-07-23-powershell-special-characters/</link><pubDate>Thu, 23 Jul 2009 00:00:00 +0000</pubDate><author>neolisk@gmail.com (Victor Zakharov)</author><atom:modified>Sat, 28 Jan 2023 07:26:02 -0500</atom:modified><guid>/posts/2009-07-23-powershell-special-characters/</guid><description>This article originally started when I was actively involved in PowerShell development.
Now I am mostly updating it on request of my blog readers (people like you).
Are you looking for a character combination not listed here? Found a mistake? Let me know at neolisk@gmail.com.
$ (dollar sign) Declare/use a variable. Powershell has a number of built-in variables (thanks to Rein), such as $null, $true or $false. These are case insensitive and usually read-only, either explicitly (they will produce an error if you try to reassign them), or silently ignoring any updates.</description><dc:creator>Victor Zakharov</dc:creator><category>powershell</category></item></channel></rss>