<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: need help converting a perl script to powershell in Microsoft Virtualization Discussions</title>
    <link>https://community.netapp.com/t5/Microsoft-Virtualization-Discussions/need-help-converting-a-perl-script-to-powershell/m-p/31298#M1461</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have not been able to get this working in my lab. I have install v1.4 of the toolkit but I get errors when trying to run the script.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;New-Object : Cannot find type &lt;A href="Collections.HashTable           " target="_blank"&gt;&lt;/A&gt;: make sure the assembly containing this type is loaded&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;At C:\scripts\check_cpu.ps1:4 char:20&lt;/P&gt;&lt;P&gt;+ $first = New-Object &amp;lt;&amp;lt;&amp;lt;&amp;lt;  Collections.HashTable&lt;/P&gt;&lt;P&gt;    + CategoryInfo          : InvalidType: (&lt;SPAN __jive_emoticon_name="happy"&gt;&lt;/SPAN&gt; &lt;A href="https://community.netapp.com/New-Object" target="_blank"&gt;&lt;/A&gt;, PSArgumentException&lt;/P&gt;&lt;P&gt;    + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;New-Object : Cannot find type &lt;A href="Collections.HashTable           " target="_blank"&gt;&lt;/A&gt;: make sure the assembly containing this type is loaded&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;At C:\scripts\check_cpu.ps1:5 char:21&lt;/P&gt;&lt;P&gt;+ $second = New-Object &amp;lt;&amp;lt;&amp;lt;&amp;lt;  Collections.HashTable&lt;/P&gt;&lt;P&gt;    + CategoryInfo          : InvalidType: (&lt;SPAN __jive_emoticon_name="happy"&gt;&lt;/SPAN&gt; &lt;A href="https://community.netapp.com/New-Object" target="_blank"&gt;&lt;/A&gt;, PSArgumentException&lt;/P&gt;&lt;P&gt;    + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You cannot call a method on a null-valued expression.&lt;/P&gt;&lt;P&gt;At C:\scripts\check_cpu.ps1:13 char:25&lt;/P&gt;&lt;P&gt;+         &lt;A href="https://community.netapp.com/void" target="_blank"&gt;&lt;/A&gt;$first.add &amp;lt;&amp;lt;&amp;lt;&amp;lt; ($_.Name,$_.value)&lt;/P&gt;&lt;P&gt;    + CategoryInfo          : InvalidOperation: (add:String) [], RuntimeException&lt;/P&gt;&lt;P&gt;    + FullyQualifiedErrorId : InvokeMethodOnNull&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You cannot call a method on a null-valued expression.&lt;/P&gt;&lt;P&gt;At C:\scripts\check_cpu.ps1:13 char:25&lt;/P&gt;&lt;P&gt;+         &lt;A href="https://community.netapp.com/void" target="_blank"&gt;&lt;/A&gt;$first.add &amp;lt;&amp;lt;&amp;lt;&amp;lt; ($_.Name,$_.value)&lt;/P&gt;&lt;P&gt;    + CategoryInfo          : InvalidOperation: (add:String) [], RuntimeException&lt;/P&gt;&lt;P&gt;    + FullyQualifiedErrorId : InvokeMethodOnNull&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You cannot call a method on a null-valued expression.&lt;/P&gt;&lt;P&gt;At C:\scripts\check_cpu.ps1:13 char:25&lt;/P&gt;&lt;P&gt;+         &lt;A href="https://community.netapp.com/void" target="_blank"&gt;&lt;/A&gt;$first.add &amp;lt;&amp;lt;&amp;lt;&amp;lt; ($_.Name,$_.value)&lt;/P&gt;&lt;P&gt;    + CategoryInfo          : InvalidOperation: (add:String) [], RuntimeException&lt;/P&gt;&lt;P&gt;    + FullyQualifiedErrorId : InvokeMethodOnNull&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You cannot call a method on a null-valued expression.&lt;/P&gt;&lt;P&gt;At C:\scripts\check_cpu.ps1:13 char:25&lt;/P&gt;&lt;P&gt;+         &lt;A href="https://community.netapp.com/void" target="_blank"&gt;&lt;/A&gt;$first.add &amp;lt;&amp;lt;&amp;lt;&amp;lt; ($_.Name,$_.value)&lt;/P&gt;&lt;P&gt;    + CategoryInfo          : InvalidOperation: (add:String) [], RuntimeException&lt;/P&gt;&lt;P&gt;    + FullyQualifiedErrorId : InvokeMethodOnNull&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You cannot call a method on a null-valued expression.&lt;/P&gt;&lt;P&gt;At C:\scripts\check_cpu.ps1:24 char:26&lt;/P&gt;&lt;P&gt;+         &lt;A href="https://community.netapp.com/void" target="_blank"&gt;&lt;/A&gt;$second.add &amp;lt;&amp;lt;&amp;lt;&amp;lt; ($_.Name,$_.value)&lt;/P&gt;&lt;P&gt;    + CategoryInfo          : InvalidOperation: (add:String) [], RuntimeException&lt;/P&gt;&lt;P&gt;    + FullyQualifiedErrorId : InvokeMethodOnNull&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You cannot call a method on a null-valued expression.&lt;/P&gt;&lt;P&gt;At C:\scripts\check_cpu.ps1:24 char:26&lt;/P&gt;&lt;P&gt;+         &lt;A href="https://community.netapp.com/void" target="_blank"&gt;&lt;/A&gt;$second.add &amp;lt;&amp;lt;&amp;lt;&amp;lt; ($_.Name,$_.value)&lt;/P&gt;&lt;P&gt;    + CategoryInfo          : InvalidOperation: (add:String) [], RuntimeException&lt;/P&gt;&lt;P&gt;    + FullyQualifiedErrorId : InvokeMethodOnNull&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You cannot call a method on a null-valued expression.&lt;/P&gt;&lt;P&gt;At C:\scripts\check_cpu.ps1:24 char:26&lt;/P&gt;&lt;P&gt;+         &lt;A href="https://community.netapp.com/void" target="_blank"&gt;&lt;/A&gt;$second.add &amp;lt;&amp;lt;&amp;lt;&amp;lt; ($_.Name,$_.value)&lt;/P&gt;&lt;P&gt;    + CategoryInfo          : InvalidOperation: (add:String) [], RuntimeException&lt;/P&gt;&lt;P&gt;    + FullyQualifiedErrorId : InvokeMethodOnNull&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You cannot call a method on a null-valued expression.&lt;/P&gt;&lt;P&gt;At C:\scripts\check_cpu.ps1:24 char:26&lt;/P&gt;&lt;P&gt;+         &lt;A href="https://community.netapp.com/void" target="_blank"&gt;&lt;/A&gt;$second.add &amp;lt;&amp;lt;&amp;lt;&amp;lt; ($_.Name,$_.value)&lt;/P&gt;&lt;P&gt;    + CategoryInfo          : InvalidOperation: (add:String) [], RuntimeException&lt;/P&gt;&lt;P&gt;    + FullyQualifiedErrorId : InvokeMethodOnNull&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Attempted to divide by zero.&lt;/P&gt;&lt;P&gt;At C:\scripts\check_cpu.ps1:27 char:78&lt;/P&gt;&lt;P&gt;+ $totalCpuBusy = ($second.total_processor_busy - $first.total_processor_busy)/ &amp;lt;&amp;lt;&amp;lt;&amp;lt; ($second.cpu_elapsed_time - $first&lt;/P&gt;&lt;P&gt;.cpu_elapsed_time)&lt;/P&gt;&lt;P&gt;    + CategoryInfo          : NotSpecified: (&lt;SPAN __jive_emoticon_name="happy"&gt;&lt;/SPAN&gt; [], RuntimeException&lt;/P&gt;&lt;P&gt;    + FullyQualifiedErrorId : RuntimeException&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Attempted to divide by zero.&lt;/P&gt;&lt;P&gt;At C:\scripts\check_cpu.ps1:28 char:76&lt;/P&gt;&lt;P&gt;+ $averageCpuBusy = ($second.avg_processor_busy - $first.avg_processor_busy)/ &amp;lt;&amp;lt;&amp;lt;&amp;lt; ($second.cpu_elapsed_time1 - $first.&lt;/P&gt;&lt;P&gt;cpu_elapsed_time1)&lt;/P&gt;&lt;P&gt;    + CategoryInfo          : NotSpecified: (&lt;SPAN __jive_emoticon_name="happy"&gt;&lt;/SPAN&gt; [], RuntimeException&lt;/P&gt;&lt;P&gt;    + FullyQualifiedErrorId : RuntimeException&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 19 Jul 2011 04:52:14 GMT</pubDate>
    <dc:creator>bever</dc:creator>
    <dc:date>2011-07-19T04:52:14Z</dc:date>
    <item>
      <title>need help converting a perl script to powershell</title>
      <link>https://community.netapp.com/t5/Microsoft-Virtualization-Discussions/need-help-converting-a-perl-script-to-powershell/m-p/31286#M1459</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have a perl script which call an optap command via rsh and parses the output. If a certain condition is met, it calls another ontap via rsh to run a couple more ontap commands.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am looking for help converting this to powershell and potentially getting rid of the rsh dependency if possible.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Can you help?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 05 Jun 2025 06:50:22 GMT</pubDate>
      <guid>https://community.netapp.com/t5/Microsoft-Virtualization-Discussions/need-help-converting-a-perl-script-to-powershell/m-p/31286#M1459</guid>
      <dc:creator>bever</dc:creator>
      <dc:date>2025-06-05T06:50:22Z</dc:date>
    </item>
    <item>
      <title>need help converting a perl script to powershell</title>
      <link>https://community.netapp.com/t5/Microsoft-Virtualization-Discussions/need-help-converting-a-perl-script-to-powershell/m-p/31290#M1460</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hey Bever, Try something like:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="PowerShellColorizedScript"&gt;&lt;SPAN style="color: #ff4500;"&gt;$interval&lt;/SPAN&gt; &lt;SPAN style="color: #a9a9a9;"&gt;=&lt;/SPAN&gt; &lt;SPAN style="color: #800080;"&gt;5&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&lt;SPAN style="color: #ff4500;"&gt;$Threshold&lt;/SPAN&gt; &lt;SPAN style="color: #a9a9a9;"&gt;=&lt;/SPAN&gt; &lt;SPAN style="color: #800080;"&gt;99&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&lt;SPAN style="color: #ff4500;"&gt;$Controller&lt;/SPAN&gt; &lt;SPAN style="color: #a9a9a9;"&gt;=&lt;/SPAN&gt; &lt;SPAN style="color: #0000ff;"&gt;Connect-NaController&lt;/SPAN&gt; &lt;SPAN style="color: #8b0000;"&gt;'192.168.1.91'&lt;/SPAN&gt; &lt;SPAN style="color: #000080;"&gt;-Credential&lt;/SPAN&gt; &lt;SPAN style="color: #8a2be2;"&gt;root&lt;/SPAN&gt; &lt;SPAN style="color: #000080;"&gt;-Transient&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&lt;SPAN style="color: #ff4500;"&gt;$first&lt;/SPAN&gt; &lt;SPAN style="color: #a9a9a9;"&gt;=&lt;/SPAN&gt; &lt;SPAN style="color: #0000ff;"&gt;New-Object&lt;/SPAN&gt; &lt;SPAN style="color: #8a2be2;"&gt;Collections.HashTable&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&lt;SPAN style="color: #ff4500;"&gt;$second&lt;/SPAN&gt; &lt;SPAN style="color: #a9a9a9;"&gt;=&lt;/SPAN&gt; &lt;SPAN style="color: #0000ff;"&gt;New-Object&lt;/SPAN&gt; &lt;SPAN style="color: #8a2be2;"&gt;Collections.HashTable&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&lt;SPAN style="color: #0000ff;"&gt;Get-NaPerfData&lt;/SPAN&gt; &lt;SPAN style="color: #000080;"&gt;-Name&lt;/SPAN&gt; &lt;SPAN style="color: #8a2be2;"&gt;System&lt;/SPAN&gt; &lt;SPAN style="color: #000000;"&gt;`
&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #000080;"&gt;-Instances&lt;/SPAN&gt; &lt;SPAN style="color: #8a2be2;"&gt;system&lt;/SPAN&gt; &lt;SPAN style="color: #000000;"&gt;`
&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #000080;"&gt;-Controller&lt;/SPAN&gt; &lt;SPAN style="color: #ff4500;"&gt;$Controller&lt;/SPAN&gt; &lt;SPAN style="color: #000000;"&gt;`
&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #000080;"&gt;-Counters&lt;/SPAN&gt; &lt;SPAN style="color: #8b0000;"&gt;'total_processor_busy'&lt;/SPAN&gt;&lt;SPAN style="color: #a9a9a9;"&gt;,&lt;/SPAN&gt;&lt;SPAN style="color: #8b0000;"&gt;'cpu_elapsed_time'&lt;/SPAN&gt;&lt;SPAN style="color: #a9a9a9;"&gt;,&lt;/SPAN&gt;&lt;SPAN style="color: #8b0000;"&gt;'avg_processor_busy'&lt;/SPAN&gt;&lt;SPAN style="color: #a9a9a9;"&gt;,&lt;/SPAN&gt;&lt;SPAN style="color: #8b0000;"&gt;'cpu_elapsed_time1'&lt;/SPAN&gt;&lt;SPAN style="color: #a9a9a9;"&gt;|&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #0000ff;"&gt;Select-Object&lt;/SPAN&gt; &lt;SPAN style="color: #000080;"&gt;-ExpandProperty&lt;/SPAN&gt; &lt;SPAN style="color: #8a2be2;"&gt;Counters&lt;/SPAN&gt; &lt;SPAN style="color: #a9a9a9;"&gt;|&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #0000ff;"&gt;ForEach-Object&lt;/SPAN&gt; &lt;SPAN style="color: #000000;"&gt;{&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #008080;"&gt;[void]&lt;/SPAN&gt;&lt;SPAN style="color: #ff4500;"&gt;$first&lt;/SPAN&gt;&lt;SPAN style="color: #a9a9a9;"&gt;.&lt;/SPAN&gt;&lt;SPAN style="color: #000000;"&gt;add&lt;/SPAN&gt;&lt;SPAN style="color: #000000;"&gt;(&lt;/SPAN&gt;&lt;SPAN style="color: #ff4500;"&gt;$_&lt;/SPAN&gt;&lt;SPAN style="color: #a9a9a9;"&gt;.&lt;/SPAN&gt;&lt;SPAN style="color: #000000;"&gt;Name&lt;/SPAN&gt;&lt;SPAN style="color: #a9a9a9;"&gt;,&lt;/SPAN&gt;&lt;SPAN style="color: #ff4500;"&gt;$_&lt;/SPAN&gt;&lt;SPAN style="color: #a9a9a9;"&gt;.&lt;/SPAN&gt;&lt;SPAN style="color: #000000;"&gt;value&lt;/SPAN&gt;&lt;SPAN style="color: #000000;"&gt;)&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #000000;"&gt;}&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&lt;SPAN style="color: #0000ff;"&gt;Start-Sleep&lt;/SPAN&gt; &lt;SPAN style="color: #000080;"&gt;-Seconds&lt;/SPAN&gt; &lt;SPAN style="color: #ff4500;"&gt;$interval&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&lt;SPAN style="color: #0000ff;"&gt;Get-NaPerfData&lt;/SPAN&gt; &lt;SPAN style="color: #000080;"&gt;-Name&lt;/SPAN&gt; &lt;SPAN style="color: #8a2be2;"&gt;System&lt;/SPAN&gt; &lt;SPAN style="color: #000000;"&gt;`
&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #000080;"&gt;-Instances&lt;/SPAN&gt; &lt;SPAN style="color: #8a2be2;"&gt;system&lt;/SPAN&gt; &lt;SPAN style="color: #000000;"&gt;`
&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #000080;"&gt;-Controller&lt;/SPAN&gt; &lt;SPAN style="color: #ff4500;"&gt;$Controller&lt;/SPAN&gt; &lt;SPAN style="color: #000000;"&gt;`
&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #000080;"&gt;-Counters&lt;/SPAN&gt; &lt;SPAN style="color: #8b0000;"&gt;'total_processor_busy'&lt;/SPAN&gt;&lt;SPAN style="color: #a9a9a9;"&gt;,&lt;/SPAN&gt;&lt;SPAN style="color: #8b0000;"&gt;'cpu_elapsed_time'&lt;/SPAN&gt;&lt;SPAN style="color: #a9a9a9;"&gt;,&lt;/SPAN&gt;&lt;SPAN style="color: #8b0000;"&gt;'avg_processor_busy'&lt;/SPAN&gt;&lt;SPAN style="color: #a9a9a9;"&gt;,&lt;/SPAN&gt;&lt;SPAN style="color: #8b0000;"&gt;'cpu_elapsed_time1'&lt;/SPAN&gt; &lt;SPAN style="color: #a9a9a9;"&gt;|&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #0000ff;"&gt;Select-Object&lt;/SPAN&gt; &lt;SPAN style="color: #000080;"&gt;-ExpandProperty&lt;/SPAN&gt; &lt;SPAN style="color: #8a2be2;"&gt;Counters&lt;/SPAN&gt; &lt;SPAN style="color: #a9a9a9;"&gt;|&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #0000ff;"&gt;ForEach-Object&lt;/SPAN&gt; &lt;SPAN style="color: #000000;"&gt;{&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #008080;"&gt;[void]&lt;/SPAN&gt;&lt;SPAN style="color: #ff4500;"&gt;$second&lt;/SPAN&gt;&lt;SPAN style="color: #a9a9a9;"&gt;.&lt;/SPAN&gt;&lt;SPAN style="color: #000000;"&gt;add&lt;/SPAN&gt;&lt;SPAN style="color: #000000;"&gt;(&lt;/SPAN&gt;&lt;SPAN style="color: #ff4500;"&gt;$_&lt;/SPAN&gt;&lt;SPAN style="color: #a9a9a9;"&gt;.&lt;/SPAN&gt;&lt;SPAN style="color: #000000;"&gt;Name&lt;/SPAN&gt;&lt;SPAN style="color: #a9a9a9;"&gt;,&lt;/SPAN&gt;&lt;SPAN style="color: #ff4500;"&gt;$_&lt;/SPAN&gt;&lt;SPAN style="color: #a9a9a9;"&gt;.&lt;/SPAN&gt;&lt;SPAN style="color: #000000;"&gt;value&lt;/SPAN&gt;&lt;SPAN style="color: #000000;"&gt;)&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #000000;"&gt;}&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&lt;SPAN style="color: #ff4500;"&gt;$totalCpuBusy&lt;/SPAN&gt; &lt;SPAN style="color: #a9a9a9;"&gt;=&lt;/SPAN&gt; &lt;SPAN style="color: #000000;"&gt;(&lt;/SPAN&gt;&lt;SPAN style="color: #ff4500;"&gt;$second&lt;/SPAN&gt;&lt;SPAN style="color: #a9a9a9;"&gt;.&lt;/SPAN&gt;&lt;SPAN style="color: #000000;"&gt;total_processor_busy&lt;/SPAN&gt; &lt;SPAN style="color: #a9a9a9;"&gt;-&lt;/SPAN&gt; &lt;SPAN style="color: #ff4500;"&gt;$first&lt;/SPAN&gt;&lt;SPAN style="color: #a9a9a9;"&gt;.&lt;/SPAN&gt;&lt;SPAN style="color: #000000;"&gt;total_processor_busy&lt;/SPAN&gt;&lt;SPAN style="color: #000000;"&gt;)&lt;/SPAN&gt;&lt;SPAN style="color: #a9a9a9;"&gt;/&lt;/SPAN&gt;&lt;SPAN style="color: #000000;"&gt;(&lt;/SPAN&gt;&lt;SPAN style="color: #ff4500;"&gt;$second&lt;/SPAN&gt;&lt;SPAN style="color: #a9a9a9;"&gt;.&lt;/SPAN&gt;&lt;SPAN style="color: #000000;"&gt;cpu_elapsed_time&lt;/SPAN&gt; &lt;SPAN style="color: #a9a9a9;"&gt;-&lt;/SPAN&gt; &lt;SPAN style="color: #ff4500;"&gt;$first&lt;/SPAN&gt;&lt;SPAN style="color: #a9a9a9;"&gt;.&lt;/SPAN&gt;&lt;SPAN style="color: #000000;"&gt;cpu_elapsed_time&lt;/SPAN&gt;&lt;SPAN style="color: #000000;"&gt;)&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&lt;SPAN style="color: #ff4500;"&gt;$averageCpuBusy&lt;/SPAN&gt; &lt;SPAN style="color: #a9a9a9;"&gt;=&lt;/SPAN&gt; &lt;SPAN style="color: #000000;"&gt;(&lt;/SPAN&gt;&lt;SPAN style="color: #ff4500;"&gt;$second&lt;/SPAN&gt;&lt;SPAN style="color: #a9a9a9;"&gt;.&lt;/SPAN&gt;&lt;SPAN style="color: #000000;"&gt;avg_processor_busy&lt;/SPAN&gt; &lt;SPAN style="color: #a9a9a9;"&gt;-&lt;/SPAN&gt; &lt;SPAN style="color: #ff4500;"&gt;$first&lt;/SPAN&gt;&lt;SPAN style="color: #a9a9a9;"&gt;.&lt;/SPAN&gt;&lt;SPAN style="color: #000000;"&gt;avg_processor_busy&lt;/SPAN&gt;&lt;SPAN style="color: #000000;"&gt;)&lt;/SPAN&gt;&lt;SPAN style="color: #a9a9a9;"&gt;/&lt;/SPAN&gt;&lt;SPAN style="color: #000000;"&gt;(&lt;/SPAN&gt;&lt;SPAN style="color: #ff4500;"&gt;$second&lt;/SPAN&gt;&lt;SPAN style="color: #a9a9a9;"&gt;.&lt;/SPAN&gt;&lt;SPAN style="color: #000000;"&gt;cpu_elapsed_time1&lt;/SPAN&gt; &lt;SPAN style="color: #a9a9a9;"&gt;-&lt;/SPAN&gt; &lt;SPAN style="color: #ff4500;"&gt;$first&lt;/SPAN&gt;&lt;SPAN style="color: #a9a9a9;"&gt;.&lt;/SPAN&gt;&lt;SPAN style="color: #000000;"&gt;cpu_elapsed_time1&lt;/SPAN&gt;&lt;SPAN style="color: #000000;"&gt;)&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&lt;SPAN style="color: #00008b;"&gt;if&lt;/SPAN&gt; &lt;SPAN style="color: #000000;"&gt;(&lt;/SPAN&gt;&lt;SPAN style="color: #ff4500;"&gt;$averageCpuBusy&lt;/SPAN&gt; &lt;SPAN style="color: #a9a9a9;"&gt;-gt&lt;/SPAN&gt; &lt;SPAN style="color: #ff4500;"&gt;$threshold&lt;/SPAN&gt;&lt;SPAN style="color: #000000;"&gt;)&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&lt;SPAN style="color: #000000;"&gt;{&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #0000ff;"&gt;Invoke-NaSsh&lt;/SPAN&gt; &lt;SPAN style="color: #8b0000;"&gt;"priv set diag; prof off; prof reset; prof on"&lt;/SPAN&gt; &lt;SPAN style="color: #000080;"&gt;-Controller&lt;/SPAN&gt; &lt;SPAN style="color: #ff4500;"&gt;$Controller&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #0000ff;"&gt;Start-Sleep&lt;/SPAN&gt; &lt;SPAN style="color: #000080;"&gt;-Seconds&lt;/SPAN&gt; &lt;SPAN style="color: #800080;"&gt;60&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #0000ff;"&gt;Invoke-NaSsh&lt;/SPAN&gt; &lt;SPAN style="color: #8b0000;"&gt;"priv set diag; prof dump"&lt;/SPAN&gt; &lt;SPAN style="color: #000080;"&gt;-Controller&lt;/SPAN&gt; &lt;SPAN style="color: #ff4500;"&gt;$Controller&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&lt;SPAN style="color: #000000;"&gt;}&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&lt;/PRE&gt;&lt;P&gt;Hope that helps, &lt;/P&gt;&lt;P&gt;~Glenn&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 16 Jul 2011 16:37:26 GMT</pubDate>
      <guid>https://community.netapp.com/t5/Microsoft-Virtualization-Discussions/need-help-converting-a-perl-script-to-powershell/m-p/31290#M1460</guid>
      <dc:creator>GlennSizemore</dc:creator>
      <dc:date>2011-07-16T16:37:26Z</dc:date>
    </item>
    <item>
      <title>Re: need help converting a perl script to powershell</title>
      <link>https://community.netapp.com/t5/Microsoft-Virtualization-Discussions/need-help-converting-a-perl-script-to-powershell/m-p/31298#M1461</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have not been able to get this working in my lab. I have install v1.4 of the toolkit but I get errors when trying to run the script.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;New-Object : Cannot find type &lt;A href="Collections.HashTable           " target="_blank"&gt;&lt;/A&gt;: make sure the assembly containing this type is loaded&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;At C:\scripts\check_cpu.ps1:4 char:20&lt;/P&gt;&lt;P&gt;+ $first = New-Object &amp;lt;&amp;lt;&amp;lt;&amp;lt;  Collections.HashTable&lt;/P&gt;&lt;P&gt;    + CategoryInfo          : InvalidType: (&lt;SPAN __jive_emoticon_name="happy"&gt;&lt;/SPAN&gt; &lt;A href="https://community.netapp.com/New-Object" target="_blank"&gt;&lt;/A&gt;, PSArgumentException&lt;/P&gt;&lt;P&gt;    + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;New-Object : Cannot find type &lt;A href="Collections.HashTable           " target="_blank"&gt;&lt;/A&gt;: make sure the assembly containing this type is loaded&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;At C:\scripts\check_cpu.ps1:5 char:21&lt;/P&gt;&lt;P&gt;+ $second = New-Object &amp;lt;&amp;lt;&amp;lt;&amp;lt;  Collections.HashTable&lt;/P&gt;&lt;P&gt;    + CategoryInfo          : InvalidType: (&lt;SPAN __jive_emoticon_name="happy"&gt;&lt;/SPAN&gt; &lt;A href="https://community.netapp.com/New-Object" target="_blank"&gt;&lt;/A&gt;, PSArgumentException&lt;/P&gt;&lt;P&gt;    + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You cannot call a method on a null-valued expression.&lt;/P&gt;&lt;P&gt;At C:\scripts\check_cpu.ps1:13 char:25&lt;/P&gt;&lt;P&gt;+         &lt;A href="https://community.netapp.com/void" target="_blank"&gt;&lt;/A&gt;$first.add &amp;lt;&amp;lt;&amp;lt;&amp;lt; ($_.Name,$_.value)&lt;/P&gt;&lt;P&gt;    + CategoryInfo          : InvalidOperation: (add:String) [], RuntimeException&lt;/P&gt;&lt;P&gt;    + FullyQualifiedErrorId : InvokeMethodOnNull&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You cannot call a method on a null-valued expression.&lt;/P&gt;&lt;P&gt;At C:\scripts\check_cpu.ps1:13 char:25&lt;/P&gt;&lt;P&gt;+         &lt;A href="https://community.netapp.com/void" target="_blank"&gt;&lt;/A&gt;$first.add &amp;lt;&amp;lt;&amp;lt;&amp;lt; ($_.Name,$_.value)&lt;/P&gt;&lt;P&gt;    + CategoryInfo          : InvalidOperation: (add:String) [], RuntimeException&lt;/P&gt;&lt;P&gt;    + FullyQualifiedErrorId : InvokeMethodOnNull&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You cannot call a method on a null-valued expression.&lt;/P&gt;&lt;P&gt;At C:\scripts\check_cpu.ps1:13 char:25&lt;/P&gt;&lt;P&gt;+         &lt;A href="https://community.netapp.com/void" target="_blank"&gt;&lt;/A&gt;$first.add &amp;lt;&amp;lt;&amp;lt;&amp;lt; ($_.Name,$_.value)&lt;/P&gt;&lt;P&gt;    + CategoryInfo          : InvalidOperation: (add:String) [], RuntimeException&lt;/P&gt;&lt;P&gt;    + FullyQualifiedErrorId : InvokeMethodOnNull&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You cannot call a method on a null-valued expression.&lt;/P&gt;&lt;P&gt;At C:\scripts\check_cpu.ps1:13 char:25&lt;/P&gt;&lt;P&gt;+         &lt;A href="https://community.netapp.com/void" target="_blank"&gt;&lt;/A&gt;$first.add &amp;lt;&amp;lt;&amp;lt;&amp;lt; ($_.Name,$_.value)&lt;/P&gt;&lt;P&gt;    + CategoryInfo          : InvalidOperation: (add:String) [], RuntimeException&lt;/P&gt;&lt;P&gt;    + FullyQualifiedErrorId : InvokeMethodOnNull&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You cannot call a method on a null-valued expression.&lt;/P&gt;&lt;P&gt;At C:\scripts\check_cpu.ps1:24 char:26&lt;/P&gt;&lt;P&gt;+         &lt;A href="https://community.netapp.com/void" target="_blank"&gt;&lt;/A&gt;$second.add &amp;lt;&amp;lt;&amp;lt;&amp;lt; ($_.Name,$_.value)&lt;/P&gt;&lt;P&gt;    + CategoryInfo          : InvalidOperation: (add:String) [], RuntimeException&lt;/P&gt;&lt;P&gt;    + FullyQualifiedErrorId : InvokeMethodOnNull&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You cannot call a method on a null-valued expression.&lt;/P&gt;&lt;P&gt;At C:\scripts\check_cpu.ps1:24 char:26&lt;/P&gt;&lt;P&gt;+         &lt;A href="https://community.netapp.com/void" target="_blank"&gt;&lt;/A&gt;$second.add &amp;lt;&amp;lt;&amp;lt;&amp;lt; ($_.Name,$_.value)&lt;/P&gt;&lt;P&gt;    + CategoryInfo          : InvalidOperation: (add:String) [], RuntimeException&lt;/P&gt;&lt;P&gt;    + FullyQualifiedErrorId : InvokeMethodOnNull&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You cannot call a method on a null-valued expression.&lt;/P&gt;&lt;P&gt;At C:\scripts\check_cpu.ps1:24 char:26&lt;/P&gt;&lt;P&gt;+         &lt;A href="https://community.netapp.com/void" target="_blank"&gt;&lt;/A&gt;$second.add &amp;lt;&amp;lt;&amp;lt;&amp;lt; ($_.Name,$_.value)&lt;/P&gt;&lt;P&gt;    + CategoryInfo          : InvalidOperation: (add:String) [], RuntimeException&lt;/P&gt;&lt;P&gt;    + FullyQualifiedErrorId : InvokeMethodOnNull&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You cannot call a method on a null-valued expression.&lt;/P&gt;&lt;P&gt;At C:\scripts\check_cpu.ps1:24 char:26&lt;/P&gt;&lt;P&gt;+         &lt;A href="https://community.netapp.com/void" target="_blank"&gt;&lt;/A&gt;$second.add &amp;lt;&amp;lt;&amp;lt;&amp;lt; ($_.Name,$_.value)&lt;/P&gt;&lt;P&gt;    + CategoryInfo          : InvalidOperation: (add:String) [], RuntimeException&lt;/P&gt;&lt;P&gt;    + FullyQualifiedErrorId : InvokeMethodOnNull&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Attempted to divide by zero.&lt;/P&gt;&lt;P&gt;At C:\scripts\check_cpu.ps1:27 char:78&lt;/P&gt;&lt;P&gt;+ $totalCpuBusy = ($second.total_processor_busy - $first.total_processor_busy)/ &amp;lt;&amp;lt;&amp;lt;&amp;lt; ($second.cpu_elapsed_time - $first&lt;/P&gt;&lt;P&gt;.cpu_elapsed_time)&lt;/P&gt;&lt;P&gt;    + CategoryInfo          : NotSpecified: (&lt;SPAN __jive_emoticon_name="happy"&gt;&lt;/SPAN&gt; [], RuntimeException&lt;/P&gt;&lt;P&gt;    + FullyQualifiedErrorId : RuntimeException&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Attempted to divide by zero.&lt;/P&gt;&lt;P&gt;At C:\scripts\check_cpu.ps1:28 char:76&lt;/P&gt;&lt;P&gt;+ $averageCpuBusy = ($second.avg_processor_busy - $first.avg_processor_busy)/ &amp;lt;&amp;lt;&amp;lt;&amp;lt; ($second.cpu_elapsed_time1 - $first.&lt;/P&gt;&lt;P&gt;cpu_elapsed_time1)&lt;/P&gt;&lt;P&gt;    + CategoryInfo          : NotSpecified: (&lt;SPAN __jive_emoticon_name="happy"&gt;&lt;/SPAN&gt; [], RuntimeException&lt;/P&gt;&lt;P&gt;    + FullyQualifiedErrorId : RuntimeException&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 Jul 2011 04:52:14 GMT</pubDate>
      <guid>https://community.netapp.com/t5/Microsoft-Virtualization-Discussions/need-help-converting-a-perl-script-to-powershell/m-p/31298#M1461</guid>
      <dc:creator>bever</dc:creator>
      <dc:date>2011-07-19T04:52:14Z</dc:date>
    </item>
    <item>
      <title>Re: need help converting a perl script to powershell</title>
      <link>https://community.netapp.com/t5/Microsoft-Virtualization-Discussions/need-help-converting-a-perl-script-to-powershell/m-p/31302#M1462</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt; Huh that is odd indeed.&amp;nbsp; Your throwing an error when creating the hashtable.&amp;nbsp; What happens when you run the following:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;$First = new-object collections.hashtable&lt;/P&gt;&lt;P&gt;$first.GetType()&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;~Glenn&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 Jul 2011 14:19:44 GMT</pubDate>
      <guid>https://community.netapp.com/t5/Microsoft-Virtualization-Discussions/need-help-converting-a-perl-script-to-powershell/m-p/31302#M1462</guid>
      <dc:creator>GlennSizemore</dc:creator>
      <dc:date>2011-07-19T14:19:44Z</dc:date>
    </item>
    <item>
      <title>need help converting a perl script to powershell_Please_Help</title>
      <link>https://community.netapp.com/t5/Microsoft-Virtualization-Discussions/need-help-converting-a-perl-script-to-powershell/m-p/102936#M4230</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;Can you please help me to conver this perl file to powershell?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;# modules used&lt;BR /&gt;use File::Copy;&lt;BR /&gt;use Getopt::Std;&lt;BR /&gt;use XML::Simple;&lt;BR /&gt;use Win32API::File::Time;&lt;/P&gt;&lt;P&gt;# program name and command line&lt;BR /&gt;$program_name = $0;&lt;BR /&gt;$program_name =~ s/\\/\//g;&lt;BR /&gt;$program_name = (split "/", $program_name)[-1];&lt;BR /&gt;$command_line = "$0 @ARGV";&lt;/P&gt;&lt;P&gt;# usage statement&lt;BR /&gt;$usage =&lt;BR /&gt;"Usage:&lt;/P&gt;&lt;P&gt;$program_name [-rtD] -f &amp;lt;filename&amp;gt; -m &amp;lt;metafile&amp;gt; -d &amp;lt;directory&amp;gt;&lt;BR /&gt;-c &amp;lt;configfile&amp;gt; -e &amp;lt;environment&amp;gt; -C &amp;lt;client&amp;gt;&lt;/P&gt;&lt;P&gt;Parameters:&lt;BR /&gt;-r recurse subdirectories&lt;BR /&gt;-t test mode (files copied but no files edited)&lt;BR /&gt;-D debug mode (additional output)&lt;BR /&gt;-f filename file to modify&lt;BR /&gt;-m metafile metafile with names of files to modify&lt;BR /&gt;-d directory directory with files to modify&lt;BR /&gt;-c configfile configuration file&lt;BR /&gt;-e environment environment portion of configuration file&lt;BR /&gt;-C client client portion of configuration file\n";&lt;/P&gt;&lt;P&gt;# get command line options and print usage if incorrect&lt;BR /&gt;printout ("$command_line\n");&lt;BR /&gt;$usage_error = FALSE;&lt;BR /&gt;$usage_error = TRUE unless getopts('rtDf:m:d:c:e:C:');&lt;BR /&gt;die "\n$usage" if ($usage_error eq TRUE);&lt;BR /&gt;unless (($opt_f) or ($opt_m) or ($opt_d))&lt;BR /&gt;{&lt;BR /&gt;$usage_error = TRUE;&lt;BR /&gt;$error_message = "Missing argument, at least one of -f &amp;lt;filename&amp;gt;, -m &amp;lt;metafile&amp;gt;, or -d &amp;lt;directory&amp;gt; must be specified.\n";&lt;BR /&gt;}&lt;BR /&gt;unless ($opt_c)&lt;BR /&gt;{&lt;BR /&gt;$usage_error = TRUE;&lt;BR /&gt;$error_message = "Missing argument -c &amp;lt;configfile&amp;gt;.\n";&lt;BR /&gt;}&lt;BR /&gt;unless ($opt_e)&lt;BR /&gt;{&lt;BR /&gt;$usage_error = TRUE;&lt;BR /&gt;$error_message = "Missing argument -e &amp;lt;environment&amp;gt;.\n";&lt;BR /&gt;}&lt;BR /&gt;unless ($opt_C)&lt;BR /&gt;{&lt;BR /&gt;$usage_error = TRUE;&lt;BR /&gt;$error_message = "Missing argument -C &amp;lt;client&amp;gt;.\n";&lt;BR /&gt;}&lt;BR /&gt;die "${error_message}\n${usage}" if ($usage_error eq TRUE);&lt;/P&gt;&lt;P&gt;# copy command line options into well-named variables&lt;BR /&gt;$cl_recurse = (defined $opt_r) ? TRUE : FALSE;&lt;BR /&gt;$TESTMODE = (defined $opt_t) ? TRUE : FALSE;&lt;BR /&gt;$DEBUG = (defined $opt_D) ? TRUE : FALSE;&lt;BR /&gt;$cl_filename = $opt_f;&lt;BR /&gt;$cl_metafile = $opt_m;&lt;BR /&gt;$cl_directory = $opt_d;&lt;BR /&gt;$cl_configfile = $opt_c;&lt;BR /&gt;$cl_environment = $opt_e;&lt;BR /&gt;$cl_client = $opt_C;&lt;/P&gt;&lt;P&gt;# add the "-f" parameter to the filearr&lt;BR /&gt;@filearr = ($cl_filename)&lt;BR /&gt;if (defined $cl_filename);&lt;BR /&gt;# add the file contents of the "-m" parameter to the filearr&lt;BR /&gt;push @filearr, read_metafile ($cl_metafile, $cl_recurse)&lt;BR /&gt;if (defined $cl_metafile);&lt;BR /&gt;# add the directory contents of the "-d" parameter to the filearr&lt;BR /&gt;push @filearr, read_directory ($cl_directory, $cl_recurse)&lt;BR /&gt;if (defined $cl_directory);&lt;BR /&gt;# read in the configuration data&lt;BR /&gt;$confighashref = read_config ($cl_configfile, $cl_environment, $cl_client)&lt;BR /&gt;if (defined $cl_configfile);&lt;BR /&gt;# perform the replacement operations on the filearr&lt;BR /&gt;$rc = string_replace($confighashref, @filearr);&lt;/P&gt;&lt;P&gt;# indicate success or failure&lt;BR /&gt;die "ERROR: $program_name returned $rc!\n" if ($rc);&lt;BR /&gt;print "$program_name completed.\n" unless ($rc);&lt;/P&gt;&lt;P&gt;### end of main body&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;### subroutines&lt;/P&gt;&lt;P&gt;# sub read_metafile ($$)&lt;BR /&gt;# PARAMETERS: scalar metafile - name of the metafile&lt;BR /&gt;# scalar recurse - flag for recursing into subdirectories&lt;BR /&gt;# RETURNCODE: list - files contained in the metafile&lt;BR /&gt;# DESCRIPTION:&lt;BR /&gt;# this subroutine opens a metafile passed as a paremeter and reads lines from it. if the line refers to a&lt;BR /&gt;# normal file, it is added to the return list. if the line refers to a directory, the directory contents are&lt;BR /&gt;# added via a call to read_directory. otherwise, a warning is printed (shouldn't explicitly list items that are&lt;BR /&gt;# not normal files or not directories).&lt;/P&gt;&lt;P&gt;sub read_metafile($$)&lt;BR /&gt;{&lt;BR /&gt;# parameters&lt;BR /&gt;my $metafile = shift;&lt;BR /&gt;my $recurse = shift;&lt;BR /&gt;# local variables&lt;BR /&gt;my @readfiles = ();&lt;BR /&gt;my $readitem = "";&lt;BR /&gt;debugout ("read_metafile: called with \"$metafile\"\n");&lt;/P&gt;&lt;P&gt;# open the metafile or error and return an empty list&lt;BR /&gt;unless (open METAFILEHANDLE, $metafile)&lt;BR /&gt;{&lt;BR /&gt;print "WARNING: unable to open metafile \"$metafile\"!\n";&lt;BR /&gt;return ();&lt;BR /&gt;}## end unless&lt;/P&gt;&lt;P&gt;# read items from the metafile&lt;BR /&gt;while ($readitem = &amp;lt;METAFILEHANDLE&amp;gt;)&lt;BR /&gt;{&lt;BR /&gt;# strip any newline&lt;BR /&gt;chomp $readitem;&lt;BR /&gt;# skip empty lines&lt;BR /&gt;next if $readitem =~ /^\s*$/;&lt;BR /&gt;# add normal files&lt;BR /&gt;if (-f $readitem)&lt;BR /&gt;{&lt;BR /&gt;debugout ("read_metafile: added file \"$readitem\"\n");&lt;BR /&gt;push @readfiles, $readitem;&lt;BR /&gt;}## end if&lt;/P&gt;&lt;P&gt;# add contents of directories&lt;BR /&gt;elsif (-d $readitem)&lt;BR /&gt;{&lt;BR /&gt;debugout ("read_metafile: adding directory \"$readitem\"\n");&lt;BR /&gt;push @readfiles, read_directory ($readitem, $recurse);&lt;BR /&gt;}## end elsif&lt;/P&gt;&lt;P&gt;# warn on anything else (shouldn't be in metafile)&lt;BR /&gt;else&lt;BR /&gt;{&lt;BR /&gt;print "WARNING: item \"$readitem\" skipped!\n";&lt;BR /&gt;}## end else&lt;/P&gt;&lt;P&gt;}## end while&lt;/P&gt;&lt;P&gt;# close the handle and return the list of files&lt;BR /&gt;close METAFILEHANDLE;&lt;BR /&gt;return @readfiles;&lt;BR /&gt;}## end sub read_metafile&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;# sub read_directory ($$)&lt;BR /&gt;# PARAMETERS: scalar directory - name of the directory&lt;BR /&gt;# scalar recurse - flag for recursing into subdirectories&lt;BR /&gt;# RETURNCODE: list - files contained in the directory&lt;BR /&gt;# DESCRIPTION:&lt;BR /&gt;# this subroutine opens a directory and reads all of the items it contains. directory special aliases "." and&lt;BR /&gt;# ".." are skipped. for each entry, if the item is a directory and recurse is set to true, the read_directory&lt;BR /&gt;# subroutine is recursively called on the the item, and the resulting list is added to the return list.&lt;BR /&gt;# if the item is a regular file, the item is added to the return list. otherwise, the item is skipped.&lt;/P&gt;&lt;P&gt;sub read_directory($$)&lt;BR /&gt;{&lt;BR /&gt;# parameters&lt;BR /&gt;my $directory = shift;&lt;BR /&gt;my $recurse = shift;&lt;BR /&gt;# local variables&lt;BR /&gt;my $readitem = "";&lt;BR /&gt;my @readitems = ();&lt;BR /&gt;my @readfiles = ();&lt;BR /&gt;debugout ("read_directory: called with \"$directory\", \"$recurse\"\n");&lt;/P&gt;&lt;P&gt;# skip the directory if it is one that was dynamically created in a previous run&lt;BR /&gt;if ((split "/", $directory)[-1] =~ /set\w+/)&lt;BR /&gt;{&lt;BR /&gt;debugout ("read_directory: skipping dynamically created directory \"$directory\"\n");&lt;BR /&gt;return ();&lt;BR /&gt;}## end if&lt;/P&gt;&lt;P&gt;# open the directory or warn and return an empty list&lt;BR /&gt;unless (opendir DIRHANDLE, $directory)&lt;BR /&gt;{&lt;BR /&gt;print "WARNING: unable to open directory \"$directory\"\n";&lt;BR /&gt;return ();&lt;BR /&gt;}## end unless&lt;/P&gt;&lt;P&gt;# read all of the directory contents at once and close the handle&lt;BR /&gt;# this is to avoid a large number of open handles when recursing&lt;BR /&gt;@readitems = readdir DIRHANDLE;&lt;BR /&gt;closedir DIRHANDLE;&lt;/P&gt;&lt;P&gt;# examine each item read from the directory&lt;BR /&gt;foreach $readitem (@readitems)&lt;BR /&gt;{&lt;BR /&gt;# skip "." and ".."&lt;BR /&gt;next if $readitem =~ /^\.{1,2}$/;&lt;/P&gt;&lt;P&gt;# if the item is a directory and recurse is set to TRUE, then recursively examine it&lt;BR /&gt;# and add its contents to the return list&lt;BR /&gt;if ((-d "$directory/$readitem") and ($recurse eq TRUE))&lt;BR /&gt;{&lt;BR /&gt;debugout ("read_directory: recursing into \"$readitem\"\n");&lt;BR /&gt;push @readfiles, read_directory ("$directory/$readitem", $recurse);&lt;BR /&gt;}## end if&lt;/P&gt;&lt;P&gt;# skip files that are not dependencies for the database builds&lt;BR /&gt;#elsif (($readitem !~ /\.sq[pvtrx]$/) and ($readitem !~ /\.tgt$/))&lt;BR /&gt;elsif ($readitem !~ /\.sq[pvtrx]$/)&lt;BR /&gt;{&lt;BR /&gt;debugout ("read_directory: skipping non-dbo-deployable file $readitem\n");&lt;BR /&gt;}## end elsif&lt;/P&gt;&lt;P&gt;### skip extra copies created for deployment to environments with more than one db set&lt;BR /&gt;##elsif ($readitem =~ /_modifyset[^\.]+\....$/)&lt;BR /&gt;##{&lt;BR /&gt;## debugout ("read_directory: not including auto-created file $readitem\n");&lt;BR /&gt;##}## end elsif&lt;/P&gt;&lt;P&gt;# if the item is a normal file, then add it to the return list&lt;BR /&gt;elsif (-f "$directory/$readitem")&lt;BR /&gt;{&lt;BR /&gt;debugout ("read_directory: adding file \"$readitem\"\n");&lt;BR /&gt;push @readfiles, "$directory/$readitem";&lt;BR /&gt;}## end elsif&lt;/P&gt;&lt;P&gt;# otherwise, skip it&lt;BR /&gt;else&lt;BR /&gt;{&lt;BR /&gt;debugout ("read_directory: skipping item \"$readitem\"\n");&lt;BR /&gt;}## end else&lt;/P&gt;&lt;P&gt;}## end foreach&lt;/P&gt;&lt;P&gt;# return the list of files read&lt;BR /&gt;return (@readfiles);&lt;BR /&gt;}## end sub read_directory&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;# sub read_config ($$)&lt;BR /&gt;# PARAMETERS: scalar configfile - name of the xml config file&lt;BR /&gt;# scalar env - name of the env section of the config file to load&lt;BR /&gt;# RETURNCODE: hashref - reference to a hash of configuration data&lt;BR /&gt;# DESCRIPTION:&lt;BR /&gt;# this subroutine reads in configuration data from an xml file and returns it as a hashref. the&lt;BR /&gt;# configuration data contains a hash called "op" where the keys are search patterns and the values&lt;BR /&gt;# are replacement expressions. only the op hash inside the named "env" block is returned.&lt;BR /&gt;#&lt;BR /&gt;# EXAMPLE XML DATA&amp;amp;colon; EXAMPLE HASHREF DATA (called with client="UAFC" and env="Dev"):&lt;BR /&gt;#&lt;BR /&gt;# &amp;lt;config&amp;gt; hashref =&amp;gt;&lt;BR /&gt;# &amp;lt;client name="UAFC"&amp;gt; "set" =&amp;gt;&lt;BR /&gt;# &amp;lt;env name="Dev"&amp;gt; "1" =&amp;gt;&lt;BR /&gt;# &amp;lt;set id="1"&amp;gt; "devstage" =&amp;gt; "qastage1"&lt;BR /&gt;# &amp;lt;op&amp;gt; "2" =&amp;gt;&lt;BR /&gt;# &amp;lt;search&amp;gt;devstage&amp;lt;/search&amp;gt; "devstage" =&amp;gt; "qastage2"&lt;BR /&gt;# &amp;lt;replace&amp;gt;qastage1&amp;lt;/replace&amp;gt;&lt;BR /&gt;# &amp;lt;/op&amp;gt;&lt;BR /&gt;# &amp;lt;/set&amp;gt;&lt;BR /&gt;# &amp;lt;set id="2"&amp;gt;&lt;BR /&gt;# &amp;lt;op&amp;gt;&lt;BR /&gt;# &amp;lt;search&amp;gt;devstage&amp;lt;/search&amp;gt;&lt;BR /&gt;# &amp;lt;replace&amp;gt;qastage2&amp;lt;/replace&amp;gt;&lt;BR /&gt;# &amp;lt;/op&amp;gt;&lt;BR /&gt;# &amp;lt;/set&amp;gt;&lt;BR /&gt;# &amp;lt;/env&amp;gt;&lt;BR /&gt;# &amp;lt;/client&amp;gt;&lt;BR /&gt;# &amp;lt;/config&amp;gt;&lt;/P&gt;&lt;P&gt;sub read_config($$$)&lt;BR /&gt;{&lt;BR /&gt;# parameters&lt;BR /&gt;my $configfile = shift;&lt;BR /&gt;my $env = shift;&lt;BR /&gt;my $client = shift;&lt;BR /&gt;# local variables&lt;BR /&gt;my $confighashref = "";&lt;BR /&gt;debugout ("read_config: called with \"$configfile\", \"$env\", \"$client\"\n");&lt;/P&gt;&lt;P&gt;# if the named configfile is not a normal file, warn and return NULL&lt;BR /&gt;unless (-f $configfile)&lt;BR /&gt;{&lt;BR /&gt;print "WARNING: \"$configfile\" is not a normal file!\n";&lt;BR /&gt;return "";&lt;BR /&gt;}## end unless&lt;/P&gt;&lt;P&gt;# read the entire XML file into a hash reference&lt;BR /&gt;# force "op" and "env" to be arrays, even if there is only one instance of each&lt;BR /&gt;# if an item has an attribute of "search" or "name", use that attribute value as a key in a hash&lt;BR /&gt;$confighashref = XMLin ($configfile,&lt;BR /&gt;forcearray =&amp;gt; ['op', 'env', 'set', 'client'],&lt;BR /&gt;keyattr =&amp;gt; ['search', 'name', 'id']);&lt;/P&gt;&lt;P&gt;#debugout ("confighashref:\n" . Dumper ($confighashref) . "\n");&lt;/P&gt;&lt;P&gt;# use only the env portion of the config data that matches $env&lt;BR /&gt;$confighashref = $confighashref-&amp;gt;{client}-&amp;gt;{$client}-&amp;gt;{env}-&amp;gt;{$env};&lt;/P&gt;&lt;P&gt;# warn if there is no config info at this point&lt;BR /&gt;unless ($confighashref)&lt;BR /&gt;{&lt;BR /&gt;print "WARNING: no configuration information for environment \"$env\"\n";&lt;BR /&gt;}## end unless&lt;/P&gt;&lt;P&gt;# return the configuration hash reference&lt;BR /&gt;return $confighashref;&lt;BR /&gt;}## end sub read_config&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;# sub string_replace ($@)&lt;BR /&gt;# PARAMETERS: scalar directory - name of the directory&lt;BR /&gt;# scalar recurse - flag for recursing into subdirectories&lt;BR /&gt;# RETURNCODE: list - files contained in the directory&lt;BR /&gt;# DESCRIPTION:&lt;BR /&gt;# this subroutine opens a directory and reads all of the items it contains. directory special aliases "." and&lt;BR /&gt;# ".." are skipped. for each entry, if the item is a directory and recurse is set to true, the read_directory&lt;BR /&gt;# subroutine is recursively called on the the item, and the resulting list is added to the return list.&lt;BR /&gt;# if the item is a regular file, the item is added to the return list. otherwise, the item is skipped.&lt;/P&gt;&lt;P&gt;sub string_replace($@)&lt;BR /&gt;{&lt;BR /&gt;# parameters&lt;BR /&gt;my $confighashref = shift;&lt;BR /&gt;my @filearr = @_;&lt;BR /&gt;# local variables&lt;BR /&gt;my $file = "";&lt;BR /&gt;my @linearr = ();&lt;BR /&gt;my $line = "";&lt;BR /&gt;my $pattern = "";&lt;BR /&gt;my $matched = FALSE;&lt;BR /&gt;my $set = "";&lt;BR /&gt;my @setarr = ();&lt;BR /&gt;my $setnum = 0;&lt;BR /&gt;my $newfile = "";&lt;BR /&gt;my $chompline = "";&lt;BR /&gt;my $linenumber = 0;&lt;BR /&gt;my $atime = "";&lt;BR /&gt;my $mtime = "";&lt;BR /&gt;my $ctime = "";&lt;BR /&gt;my $modified = FALSE;&lt;BR /&gt;my @newlinearr = ();&lt;BR /&gt;my $origline = "";&lt;BR /&gt;my %modlines = ();&lt;BR /&gt;my $setdir = "";&lt;BR /&gt;my %summary_hash = ();&lt;BR /&gt;##debugout ("string_replace: called with\n" . Dumper($confighashref) . "\n\"@filearr\"\n");&lt;BR /&gt;if ($DEBUG eq TRUE)&lt;BR /&gt;{&lt;BR /&gt;print "string_replace: called with\n\n";&lt;BR /&gt;require 'dumpvar.pl';&lt;BR /&gt;dumpValue ($confighashref);&lt;BR /&gt;print "\n" . (join "\n", @filearr) . "\n\n";&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;# if there are no files in the filearr, warn and return and return&lt;BR /&gt;if ($#filearr &amp;lt; 0)&lt;BR /&gt;{&lt;BR /&gt;print "WARNING: no files to modify!\n";&lt;BR /&gt;return 0;&lt;BR /&gt;}## end if&lt;/P&gt;&lt;P&gt;# if there is no data in the config hash ref, warn and return an error&lt;BR /&gt;unless (defined $confighashref)&lt;BR /&gt;{&lt;BR /&gt;print "ERROR: no configuration data!\n";&lt;BR /&gt;return 1;&lt;BR /&gt;}## end if&lt;/P&gt;&lt;P&gt;@setarr = (sort { $a &amp;lt;=&amp;gt; $b; } (keys (%{$confighashref-&amp;gt;{set}})));&lt;BR /&gt;debugout ("string_replace: setarr = " . (join " ", @setarr) . "\n");&lt;/P&gt;&lt;P&gt;# open each file in the filearr, one at a time, or warn and skip it&lt;BR /&gt;foreach $file (@filearr)&lt;BR /&gt;{&lt;BR /&gt;debugout ("\nstring_replace: checking file $file\n");&lt;/P&gt;&lt;P&gt;# capture the file times before making any changes or copies&lt;BR /&gt;($atime, $mtime, $ctime) = Win32API::File::Time::GetFileTime ($file);&lt;/P&gt;&lt;P&gt;# open the file or print a warning and skip to the next file&lt;BR /&gt;unless (open FILEHANDLE, $file)&lt;BR /&gt;{&lt;BR /&gt;print "WARNING: unable to open file $file!\n";&lt;BR /&gt;next;&lt;BR /&gt;}## end unless&lt;/P&gt;&lt;P&gt;# read in the entire contents of the file and close the handle&lt;BR /&gt;@origlinearr = &amp;lt;FILEHANDLE&amp;gt;;&lt;BR /&gt;close FILEHANDLE;&lt;/P&gt;&lt;P&gt;$setnum = 0;&lt;/P&gt;&lt;P&gt;foreach $set (@setarr)&lt;BR /&gt;{&lt;BR /&gt;$setnum++;&lt;/P&gt;&lt;P&gt;debugout ("string_replace: applying ops from set $set\n");&lt;/P&gt;&lt;P&gt;# if this is NOT the first set, copy the file and modify the copy for this set&lt;BR /&gt;if ($setnum &amp;gt; 1)&lt;BR /&gt;{&lt;BR /&gt;# use a new subdir for each additional set beyond the first&lt;BR /&gt;$setdir = $file;&lt;BR /&gt;$setdir =~ s/[^\/]+$//;&lt;BR /&gt;$setdir =~ s/\/set\w+\/$/\//;&lt;BR /&gt;$setdir .= "set$set/";&lt;/P&gt;&lt;P&gt;# construct a new unique filename for a copy of this file to be modified for this set&lt;BR /&gt;$newfile = $setdir;&lt;BR /&gt;$newfile .= (split "/", $file)[-1];&lt;/P&gt;&lt;P&gt;if ($TESTMODE eq FALSE)&lt;BR /&gt;{&lt;BR /&gt;# create the subdir if it doesn't exist&lt;BR /&gt;unless (-d $setdir)&lt;BR /&gt;{&lt;BR /&gt;unless (mkdir $setdir)&lt;BR /&gt;{&lt;BR /&gt;print "WARNING: unable to create directory $setdir: $!\n";&lt;BR /&gt;next;&lt;BR /&gt;}## end unless&lt;/P&gt;&lt;P&gt;}## end unless&lt;/P&gt;&lt;P&gt;}## end if $TESTMODE&lt;/P&gt;&lt;P&gt;else&lt;BR /&gt;{&lt;BR /&gt;debugout ("string_replace: TESTMODE eq TRUE, no directories created\n");&lt;BR /&gt;}## end else&lt;/P&gt;&lt;P&gt;# change $file to $newfile so that we are now working with the new copy&lt;BR /&gt;$file = $newfile;&lt;BR /&gt;}## end if $setnum&lt;/P&gt;&lt;P&gt;# go back to the original file data for each different set&lt;BR /&gt;## may not need this?&lt;BR /&gt;@linearr = @origlinearr;&lt;/P&gt;&lt;P&gt;# track whether the current file needs to be modified or not&lt;BR /&gt;$modified = FALSE;&lt;/P&gt;&lt;P&gt;# initialize the new file contents to nothing&lt;BR /&gt;@newlinearr = ();&lt;/P&gt;&lt;P&gt;# initialize the hash of modified lines for this file (used for logging changes)&lt;BR /&gt;%modlines = ();&lt;/P&gt;&lt;P&gt;# initialize the line number counter (used for logging changes)&lt;BR /&gt;$linenumber = 0;&lt;/P&gt;&lt;P&gt;# iterate through each line of the file, looking for any of the search strings&lt;BR /&gt;foreach $line (@linearr)&lt;BR /&gt;{&lt;BR /&gt;$linenumber++;&lt;BR /&gt;$origline = $line;&lt;BR /&gt;chomp $origline;&lt;BR /&gt;$origline =~ s/\s+/ /g;&lt;BR /&gt;$matched = FALSE;&lt;BR /&gt;foreach $search (keys %{$confighashref-&amp;gt;{set}-&amp;gt;{$set}-&amp;gt;{op}})&lt;BR /&gt;{&lt;BR /&gt;# if the line matches a search string, perform the replacement&lt;BR /&gt;if ($line =~ /$search/)&lt;BR /&gt;{&lt;BR /&gt;$replace = $confighashref-&amp;gt;{set}-&amp;gt;{$set}-&amp;gt;{op}-&amp;gt;{$search}-&amp;gt;{replace};&lt;BR /&gt;debugout ("string_replace: linenumber = $linenumber\n");&lt;BR /&gt;debugout ("string_replace: search = \"$search\"\n");&lt;BR /&gt;debugout ("string_replace: replace = \"$replace\"\n");&lt;/P&gt;&lt;P&gt;# immediately before the replacment, evaluate the replace expression&lt;BR /&gt;# this means the replace expression is perl code and may call functions/subroutines&lt;BR /&gt;# if the replace doesn't actually change the line, skip it&lt;BR /&gt;$updatedline = $line;&lt;BR /&gt;$updatedline =~ s/$search/eval $replace/ge;&lt;BR /&gt;if ($updatedline eq $line)&lt;BR /&gt;{&lt;BR /&gt;next;&lt;BR /&gt;}## end if&lt;BR /&gt;else&lt;BR /&gt;{&lt;BR /&gt;$line = $updatedline;&lt;BR /&gt;}## end else&lt;/P&gt;&lt;P&gt;if (defined $summary_hash{$replace})&lt;BR /&gt;{&lt;BR /&gt;$summary_hash{$replace}++;&lt;BR /&gt;}## end if&lt;/P&gt;&lt;P&gt;else&lt;BR /&gt;{&lt;BR /&gt;$summary_hash{$replace} = 1;&lt;BR /&gt;}## end else&lt;/P&gt;&lt;P&gt;# save the original and modified lines for later logging&lt;BR /&gt;$modlines{$linenumber}{orig} = $origline;&lt;BR /&gt;$modlines{$linenumber}{mod} = $line;&lt;/P&gt;&lt;P&gt;# indicated that the line was matched and the file was modified&lt;BR /&gt;$matched = TRUE;&lt;BR /&gt;$modified = TRUE;&lt;BR /&gt;}## end if&lt;/P&gt;&lt;P&gt;}## end foreach $search&lt;/P&gt;&lt;P&gt;# if this line was matched, save the original and modified lines for later logging&lt;BR /&gt;if ($matched eq TRUE)&lt;BR /&gt;{&lt;BR /&gt;chomp $modlines{$linenumber}{mod};&lt;BR /&gt;$modlines{$linenumber}{mod} =~ s/\s+/ /g;&lt;BR /&gt;}## end if $matched&lt;/P&gt;&lt;P&gt;# push all file lines (modified or not) back onto a new line array, for creating the new file&lt;BR /&gt;push @newlinearr, $line;&lt;BR /&gt;}## end foreach $line&lt;/P&gt;&lt;P&gt;debugout ("done, modified = $modified; testmode = $TESTMODE\n");&lt;BR /&gt;# if any of the lines were modified, rewrite the file with the modified lines&lt;BR /&gt;# if ($modified eq TRUE)&lt;BR /&gt;# {&lt;BR /&gt;if ($TESTMODE eq FALSE)&lt;BR /&gt;{&lt;BR /&gt;# open the file for writing or warn and skip to the next file&lt;BR /&gt;unless (open FILEHANDLE, "&amp;gt; $file")&lt;BR /&gt;{&lt;BR /&gt;# make the file writeable and try again&lt;BR /&gt;unless (chmod 0777, $file)&lt;BR /&gt;{&lt;BR /&gt;print "WARNING: unable to make output file writeable\n";&lt;BR /&gt;next;&lt;BR /&gt;}## end unless&lt;/P&gt;&lt;P&gt;unless (open FILEHANDLE, "&amp;gt; $file")&lt;BR /&gt;{&lt;BR /&gt;print "WARNING: unable to open file for writing: $file!\n";&lt;BR /&gt;next;&lt;BR /&gt;}## end unless&lt;/P&gt;&lt;P&gt;}## end unless&lt;/P&gt;&lt;P&gt;# print the new file contents into the file and close it&lt;BR /&gt;debugout ("string_replace: writing $file\n");&lt;BR /&gt;print FILEHANDLE @newlinearr;&lt;BR /&gt;close FILEHANDLE;&lt;BR /&gt;}&lt;BR /&gt;else&lt;BR /&gt;{&lt;BR /&gt;debugout ("string_replace: TESTMODE eq TRUE, no file saved\n");&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;if ($DEBUG eq TRUE)&lt;BR /&gt;{&lt;BR /&gt;foreach $linenumber (sort { $a &amp;lt;=&amp;gt; $b; } (keys %modlines))&lt;BR /&gt;{&lt;BR /&gt;printout (" line $linenumber\t\"");&lt;BR /&gt;printout ($modlines{$linenumber}{orig} . "\"\n");&lt;BR /&gt;printout (" chg to\t\"");&lt;BR /&gt;printout ($modlines{$linenumber}{mod} . "\"\n");&lt;BR /&gt;}## end foreach $linenumber&lt;/P&gt;&lt;P&gt;}## end if&lt;/P&gt;&lt;P&gt;# }## end if $modified&lt;/P&gt;&lt;P&gt;# restore the file times as they were before any modifications or copies&lt;BR /&gt;Win32API::File::Time::SetFileTime ($file, $atime, $mtime, $ctime);&lt;/P&gt;&lt;P&gt;}## end foreach $set&lt;/P&gt;&lt;P&gt;}## end foreach $files&lt;/P&gt;&lt;P&gt;foreach $key (keys %summary_hash)&lt;BR /&gt;{&lt;BR /&gt;print "$summary_hash{$key}\t$key\n";&lt;BR /&gt;}## end foreach&lt;/P&gt;&lt;P&gt;printout ("\n");&lt;/P&gt;&lt;P&gt;# return success&lt;BR /&gt;return 0;&lt;BR /&gt;}## end sub string_replace&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;# sub debugout ($)&lt;BR /&gt;# PARAMETERS: scalar outstring - string to (conditionally) be printed&lt;BR /&gt;# RETURNCODE: none&lt;BR /&gt;# DESCRIPTION:&lt;BR /&gt;# this subroutine calls printout on the string parameter only if the $DEBUG global variable is set to TRUE.&lt;/P&gt;&lt;P&gt;sub debugout($)&lt;BR /&gt;{&lt;BR /&gt;# parameter&lt;BR /&gt;my $outstring = shift;&lt;BR /&gt;printout ($outstring) if ($DEBUG eq TRUE);&lt;BR /&gt;}## end sub&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;# sub printout ($)&lt;BR /&gt;# PARAMETERS: scalar outstring - string to be printed&lt;BR /&gt;# RETURNCODE: none&lt;BR /&gt;# DESCRIPTION:&lt;BR /&gt;# this subroutine prints the string parameter&lt;/P&gt;&lt;P&gt;sub printout ($)&lt;BR /&gt;{&lt;BR /&gt;my $outstring = shift;&lt;BR /&gt;print $outstring;&lt;BR /&gt;}&lt;/P&gt;</description>
      <pubDate>Wed, 08 Apr 2015 21:22:40 GMT</pubDate>
      <guid>https://community.netapp.com/t5/Microsoft-Virtualization-Discussions/need-help-converting-a-perl-script-to-powershell/m-p/102936#M4230</guid>
      <dc:creator>Nickr</dc:creator>
      <dc:date>2015-04-08T21:22:40Z</dc:date>
    </item>
  </channel>
</rss>

