<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PowerShell | TekCookie</title>
	<atom:link href="https://tekcookie.com/category/powershell/feed/" rel="self" type="application/rss+xml" />
	<link>https://tekcookie.com</link>
	<description>Everything about IT</description>
	<lastBuildDate>Wed, 24 Nov 2021 14:31:43 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://i0.wp.com/tekcookie.com/wp-content/uploads/2021/06/cropped-TekCookie-211.png?fit=32%2C17&#038;ssl=1</url>
	<title>PowerShell | TekCookie</title>
	<link>https://tekcookie.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">174510684</site>	<item>
		<title>Get Local Administrator Group Members From Remote Computers</title>
		<link>https://tekcookie.com/local-administrator-group-members-from-remote-computers/</link>
					<comments>https://tekcookie.com/local-administrator-group-members-from-remote-computers/#comments</comments>
		
		<dc:creator><![CDATA[jeffythampi]]></dc:creator>
		<pubDate>Sun, 27 Jun 2021 09:33:13 +0000</pubDate>
				<category><![CDATA[Windows]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Technology]]></category>
		<guid isPermaLink="false">https://tekcookie.com/?p=3422</guid>

					<description><![CDATA[In this article, we will discuss how to get a consolidated list of local administrators group members from remote computers. We use powersell command-let invoke-command to execute the command in the remote systems. Invoke-Command is the most used command-let to execute scripts remotely. To use Invoke-Command, powershell remoting has to be enabled in the remote [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>In this article, we will discuss how to get a consolidated list of local administrators group members from remote computers.</p>


				<div class="wp-block-uagb-table-of-contents uagb-toc__align-left uagb-toc__columns-1  uagb-block-5f24a1d4     "
					data-scroll= "1"
					data-offset= "30"
					style=""
				>
				<div class="uagb-toc__wrap">
						<div class="uagb-toc__title">
							Summary of Article<br>						</div>
																<div class="uagb-toc__list-wrap">
						<ol class="uagb-toc__list"><li class="uagb-toc__list"><a href="#one-liner-to-fetch-local-administrator-group-details" class="uagb-toc-link__trigger">One liner to fetch local administrator group details</a><li class="uagb-toc__list"><a href="#script-to-fetch-the-local-administrator-group-details" class="uagb-toc-link__trigger">Script to fetch the local administrator group details</a></ol>					</div>
									</div>
				</div>
			


<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<p>We use powersell command-let invoke-command to execute the command in the remote systems. Invoke-Command is the most used command-let to execute scripts remotely.</p>



<p>To use Invoke-Command, powershell remoting has to be enabled in the remote system. Group policy can be used to deploy this feature in the organization.</p>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">One liner to fetch local administrator group details</h2>



<pre class="wp-block-code"><code lang="powershell" class="language-powershell">Invoke-Command -ScriptBlock { Get-LocalGroupMember administrators } -ComputerName pc1, pc2, pc3 | select PSComputerName, Name, SID, PrincipalSource</code></pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<p>Result:</p>



<div class="wp-block-group has-white-color has-text-color has-background is-layout-flow wp-block-group-is-layout-flow" style="background-color:#d5d5d5">
<div class="wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<pre id="block-285fd92c-5223-4dec-99b0-497b5d7c3fe9" class="wp-block-preformatted wp-block-code block-editor-block-list__block wp-block is-selected"><code><span style="color:#13608c" class="has-inline-color">PSComputerName Name                 SID                                            PrincipalSource
 -------------- ----                 ---                                            ---------------
 pc1            pc1\Administrator    S-1-5-21-1644265705-1531034170-3899888674-500  Local          
 pc1            pc1\user             S-1-5-21-1644265705-1531034170-3899888674-1002 Local          
 pc2            pc2\Administrator    S-1-5-21-1644265705-1531034170-3899899674-500  Local          
 pc3            pc3\Administrator    S-1-5-21-1644265705-1531034170-3899875674-500  Local  </span></code></pre>



<p></p>
</div>
</div>
</div>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<p></p>



<p>This one liner will connect to the respective computers mentioned in the ComputerName parameter and lists out the member details. The PrincipalSource shows whether the object belongs to local or domain. </p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>Note: As the script will connect to the remote computers, we need to run it with a user account which has access to these machines.</p></blockquote>



<p>Get-LocalGroupMember command-let is part of Microsoft.PowerShell.LocalAccounts and was made available from the version Powershell 5.1.</p>



<p>Further details of al commands are documented at <a href="https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.localaccounts/?view=powershell-5.1" target="_blank" rel="noreferrer noopener">https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.localaccounts/?view=powershell-5.1</a></p>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">Script to fetch the local administrator group details</h2>



<p>We will now wrap the above one liner to a function with the necessary checks to ensure that the computers are online by checking with a ping request.</p>



<pre class="wp-block-code"><code lang="powershell" class="language-powershell line-numbers">function Fetch-LocalAdminMembers
 {
     [CmdletBinding()]
     [Alias()]
     Param
     (
         [Parameter(Mandatory=$true)]
         [String[]]$Computers,
         [Parameter()]
         [pscredential]$Credential
     )
     begin
     {
         #Checking whether the systems are online
         #OnlineComputers variable will have all the computers which responded to ping
         $OnlineComputers = @()
         $Computers | ForEach-Object {
             if(Test-Connection $_ -Count 1 -ErrorAction SilentlyContinue) {
                 $OnlineComputers += $_
             }
         }

     }

     process
     {
         #If credentials are not passed, the script runs in current user context
         if($Credential -eq $null)
         {
             Invoke-Command -ScriptBlock {Get-LocalGroupMember administrators; Start-Sleep -Seconds 2} -ComputerName $OnlineComputers   | select PSComputerName, Name, SID, PrincipalSource
         }
         else
         {
             Invoke-Command -ScriptBlock {Get-LocalGroupMember administrators; Start-Sleep -Seconds 2} -ComputerName $OnlineComputers -Credential $Credential  | select PSComputerName, Name, SID, PrincipalSource
         }
     }

     end
     {
         $OnlineComputers = $Credential = $null
     }
 }
</code></pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<p>Executing the function</p>



<p>Fetch-LocalAdminMembers -Computers pc1, pc2</p>



<p>Result:</p>



<div class="wp-block-group has-background is-layout-flow wp-block-group-is-layout-flow" style="background-color:#d5d5d5">
<pre class="wp-block-preformatted"><code><span style="color:#13608c" class="has-inline-color">PSComputerName Name                 SID                                            PrincipalSource
 -------------- ----                 ---                                            ---------------
 pc1           pc1\Administrator    S-1-5-21-1644265705-1531034170-3899888674-500  Local          
 pc1           pc1\user             S-1-5-21-1644265705-1531034170-3899888674-1002 Local          
 pc2           pc2\Administrator    S-1-5-21-1644265705-1531034170-3894649674-500  Local          
 pc2           pc2\user2            S-1-5-21-1644265705-1531034170-<code>3894649674</code>-1002 Local </span></code> </pre>
</div>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<p>We can refine the script further for faster execution, If you can identify it, I highly recommend you to comment it with the code snippet. </p>



<p>This will be helpful for the readers and will make this more interactive.<mark class="annotation-text annotation-text-yoast" id="annotation-text-403cec01-8321-4572-8a73-ef18fa2383d0"></mark></p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>Hope you liked the article and thank you for reading</p></blockquote>
]]></content:encoded>
					
					<wfw:commentRss>https://tekcookie.com/local-administrator-group-members-from-remote-computers/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3422</post-id>	</item>
		<item>
		<title>Windows Update Information On Remote Servers with Powershell</title>
		<link>https://tekcookie.com/windows-update-information/</link>
					<comments>https://tekcookie.com/windows-update-information/#respond</comments>
		
		<dc:creator><![CDATA[jeffythampi]]></dc:creator>
		<pubDate>Fri, 18 Jun 2021 13:20:28 +0000</pubDate>
				<category><![CDATA[Windows]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Windows 10]]></category>
		<category><![CDATA[Windows Server 2016]]></category>
		<category><![CDATA[Windows update details]]></category>
		<category><![CDATA[Windows Update Information with Powershell]]></category>
		<guid isPermaLink="false">https://tekcookie.com/?p=3198</guid>

					<description><![CDATA[In this article we will see how to use powershell script to get update information of remote windows servers. Last Windows Update Information We use the com object Microsoft.Update.Session to get the update results. Below one liner will tell us the previous update search and the last update installation date. Output: LastSearchSuccessDate LastInstallationSuccessDate --------------------- --------------------------- [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>In this article we will see how to use powershell script to get update information of remote windows servers.</p>


				<div class="wp-block-uagb-table-of-contents uagb-toc__align-left uagb-toc__columns-1  uagb-block-3d8d94d9     "
					data-scroll= "1"
					data-offset= "30"
					style=""
				>
				<div class="uagb-toc__wrap">
						<div class="uagb-toc__title">
							Table Of Contents						</div>
																<div class="uagb-toc__list-wrap">
						<ol class="uagb-toc__list"><li class="uagb-toc__list"><a href="#last-windows-update-information" class="uagb-toc-link__trigger">Last Windows Update Information</a><li class="uagb-toc__list"><a href="#new-windows-update-count" class="uagb-toc-link__trigger">New Windows Update Count</a><li class="uagb-toc__list"><a href="#pending-operating-system-restart" class="uagb-toc-link__trigger">Pending Operating System Restart</a><li class="uagb-toc__list"><a href="#windows-update-status" class="uagb-toc-link__trigger">Windows Update Status</a></ol>					</div>
									</div>
				</div>
			


<div style="height:30px" aria-hidden="true" class="wp-block-spacer"></div>



<p></p>



<h2 class="wp-block-heading">Last Windows Update Information</h2>



<p>We use the com object Microsoft.Update.Session to get the update results.</p>



<p>Below one liner will tell us the previous update search and the last update installation date.</p>



<pre class="wp-block-code"><code lang="powershell" class="language-powershell">(New-Object -com "Microsoft.Update.AutoUpdate").Results
</code></pre>



<p>Output:</p>



<pre class="wp-block-preformatted">LastSearchSuccessDate LastInstallationSuccessDate
--------------------- --------------------------- 
 6/17/2021 3:54:31 AM  6/16/2021 3:57:34 AM</pre>



<div style="height:30px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">New Windows Update Count</h2>



<p>Furthermore, to get the number of updates which are yet to be installed.</p>



<pre class="wp-block-code"><code lang="powershell" class="language-powershell">$UpdateSession = New-Object -ComObject Microsoft.Update.Session
$UpdateSearcher = $UpdateSession.CreateupdateSearcher()
$Updates = @($UpdateSearcher.Search("IsInstalled=0").Updates)

#This will give the number of updates yet to install.
$Updates.Title.count </code></pre>



<div style="height:30px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">Pending Operating System Restart</h2>



<p>Any pending restart because of previous update can be identified through the registry &#8220;HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired&#8221;</p>



<p>The entry will have a value &#8220;true&#8221; if the operating system is waiting for a restart which is needed to complete an update.</p>



<div style="height:30px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">Windows Update Status</h2>



<p>Above codes can be combined to get following information about a computer. </p>



<ol class="wp-block-list"><li>LastSearchSuccessDate</li><li>LastInstallationSuccessDate</li><li>NewUpdateCount</li><li>PendingReboot</li></ol>



<p>We can now wrap the script with invoke-command to remote execute in multiple systems</p>



<pre class="wp-block-code"><code lang="powershell" class="language-powershell">function Get-WindowsUpdateInformation()
 {
     param
     (
         [Parameter()]
         [string[]]
         $ComputerName="localhost"
     )


     <code>$Results = Invoke-Command -ScriptBlock  {     </code>
         <code>$result = (New-Object -com "Microsoft.Update.AutoUpdate").Results     </code>
         <code>$UpdateSession = New-Object -ComObject Microsoft.Update.Session     </code>
         <code>$UpdateSearcher = $UpdateSession.CreateupdateSearcher()     </code>
         <code>$Updates = @($UpdateSearcher.Search("IsInstalled=0").Updates)     </code>
         <code>$PendingReboot = $false     </code>
    
         #Checking pending reboot
         <code>if (Get-Item "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired" -EA Ignore) { $PendingReboot=$true }     </code>
   
         #Framing the result to a list
         <code>New-Object psobject -Property @{         </code>
              <code>LastSearchSuccessDate = $result.LastSearchSuccessDate         </code>
              <code>LastInstallationSuccessDate = $result.LastInstallationSuccessDate         </code>
              <code>NewUpdateCount = $Updates.Title.count         </code>
              <code>PendingReboot = $PendingReboot     </code>
         <code>} </code>
     <code>} -ComputerName $ComputerName </code>

     <code>$Results | Select-Object @{Name="ServerName"; Expression={$_.PSComputerName}}, LastSearchSuccessDate, LastInstallationSuccessDate, NewUpdateCount, PendingReboot</code>
 }
 </code></pre>



<p>Multiple server/computer names can be passed as an array to get the update information, or the server name can also be read from a text file and passed as parameter.</p>



<pre id="block-a18e501e-39e5-40fa-91ad-224e419f395c" class="wp-block-preformatted">Get-WindowsUpdateInformation -ComputerName testhost1, testhost2</pre>



<p>Output:</p>



<pre class="wp-block-preformatted">ServerName LastSearchSuccessDate LastInstallationSuccessDate NewUpdateCount PendingReboot
---------- --------------------- --------------------------- -------------- -------------
 testhost1  6/17/2021 5:31:12 PM  5/16/2021 2:56:21 PM                     1         False
 testhost2  6/16/2021 6:33:22 PM  5/21/2021 4:22:34 AM                     1         False</pre>



<div style="height:30px" aria-hidden="true" class="wp-block-spacer"></div>



<p>If you have some better ideas or know other ways of doing this, please comment it. It will be informative for me and for the readers.</p>



<div style="height:30px" aria-hidden="true" class="wp-block-spacer"></div>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>Hope you liked this article and thank you for reading</p></blockquote>
]]></content:encoded>
					
					<wfw:commentRss>https://tekcookie.com/windows-update-information/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3198</post-id>	</item>
		<item>
		<title>Dynamic DNS with CloudFlare and PowerShell</title>
		<link>https://tekcookie.com/dynamic-dns-with-cloudflare-and-powershell/</link>
					<comments>https://tekcookie.com/dynamic-dns-with-cloudflare-and-powershell/#comments</comments>
		
		<dc:creator><![CDATA[jeffythampi]]></dc:creator>
		<pubDate>Sun, 14 Feb 2021 15:07:53 +0000</pubDate>
				<category><![CDATA[DNS]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Cloudflare]]></category>
		<category><![CDATA[cloudflare api]]></category>
		<category><![CDATA[cloudflare api using PowerShell]]></category>
		<category><![CDATA[DDNS]]></category>
		<category><![CDATA[Dynamic DNS]]></category>
		<guid isPermaLink="false">https://tekcookie.com/?p=1900</guid>

					<description><![CDATA[CloudFlare does provide free plan where one can add one domain and proxy the traffic for FREE. Cloudflare also do give the provision to manage the domain using API&#8217;s. We will see how to create a DNS record and update the record using PowerShell. To manage the DNS record in cloudflare, we need to obtain [&#8230;]]]></description>
										<content:encoded><![CDATA[				<div class="wp-block-uagb-table-of-contents uagb-toc__align-left uagb-toc__columns-1  uagb-block-d6b5e917     "
					data-scroll= "1"
					data-offset= "30"
					style=""
				>
				<div class="uagb-toc__wrap">
						<div class="uagb-toc__title">
							Table Of Contents						</div>
																<div class="uagb-toc__list-wrap">
						<ol class="uagb-toc__list"><li class="uagb-toc__list"><a href="#validating-cloudflare-api-key-using-powershell" class="uagb-toc-link__trigger">Validating cloudflare API key using Powershell</a><li class="uagb-toc__list"><a href="#adding-a-new-record-to-cloudflare-dns" class="uagb-toc-link__trigger">Adding a new record to Cloudflare DNS</a><li class="uagb-toc__list"><a href="#edit-an-existing-cloudflare-dns-record" class="uagb-toc-link__trigger">Edit an existing Cloudflare DNS record</a></ol>					</div>
									</div>
				</div>
			


<div style="height:30px" aria-hidden="true" class="wp-block-spacer"></div>



<p>CloudFlare does provide free plan where one can add one domain and proxy the traffic for FREE. Cloudflare also do give the provision to manage the domain using API&#8217;s.</p>



<p>We will see how to create a DNS record and update the record using PowerShell.</p>



<p>To manage the DNS record in cloudflare, we need to obtain the API token and permission to edit the DNS records.</p>



<div style="height:30px" aria-hidden="true" class="wp-block-spacer"></div>



<p>To obtain the API token, login to cloudflare, under your domain portal, click on &#8220;Get your API token&#8221;. Also note the &#8220;Zone Id&#8221; in the page, which is required in the API.</p>



<figure class="wp-block-image size-large"><img data-recalc-dims="1" fetchpriority="high" decoding="async" width="781" height="900" src="https://i0.wp.com/tekcookie.com/wp-content/uploads/2021/02/image-1.png?resize=781%2C900&#038;ssl=1" alt="" class="wp-image-2803" srcset="https://i0.wp.com/tekcookie.com/wp-content/uploads/2021/02/image-1.png?w=781&amp;ssl=1 781w, https://i0.wp.com/tekcookie.com/wp-content/uploads/2021/02/image-1.png?resize=260%2C300&amp;ssl=1 260w, https://i0.wp.com/tekcookie.com/wp-content/uploads/2021/02/image-1.png?resize=768%2C885&amp;ssl=1 768w" sizes="(max-width: 781px) 100vw, 781px" /></figure>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<p>Click on &#8220;API Tokens&#8221; tab and click &#8220;Create Token&#8221; button. </p>



<figure class="wp-block-image size-large"><img data-recalc-dims="1" decoding="async" src="https://i0.wp.com/tekcookie.com/wp-content/uploads/2021/02/image-2.png?w=1080&#038;ssl=1" alt="" class="wp-image-1904"/></figure>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<p>click the &#8220;Use Template&#8221; button as shown below.</p>



<figure class="wp-block-image size-large"><img data-recalc-dims="1" decoding="async" src="https://i0.wp.com/tekcookie.com/wp-content/uploads/2021/02/image-3.png?w=1080&#038;ssl=1" alt="" class="wp-image-1905"/></figure>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<p>Select the zone setings as below with the required domain details and click &#8220;Continue to summary&#8221;</p>



<figure class="wp-block-image size-large"><img data-recalc-dims="1" decoding="async" src="https://i0.wp.com/tekcookie.com/wp-content/uploads/2021/02/image-4.png?w=1080&#038;ssl=1" alt="" class="wp-image-1906"/></figure>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<p>Click &#8220;Create Token&#8221; to generate the api key. </p>



<figure class="wp-block-image size-large"><img data-recalc-dims="1" decoding="async" src="https://i0.wp.com/tekcookie.com/wp-content/uploads/2021/02/image-5.png?w=1080&#038;ssl=1" alt="" class="wp-image-1907"/></figure>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<p>Copy the generated key to access the DNS using PowerShell.</p>



<pre class="wp-block-preformatted">Sample Token : p6bfghn0rsdfghc-34ggb5tdas8w7ysdftj-C4
Sample ZoneID : 6cd345qefad7c71dfg5q23573017</pre>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">Validating cloudflare API key using Powershell</h2>



<p>Run the below script in PowerShell to validate the access</p>



<pre class="wp-block-code"><code lang="powershell" class="language-powershell">Invoke-RestMethod -Method Get -Uri "https://api.cloudflare.com/client/v4/user/tokens/verify" -Headers @{
 "Authorization" = "Bearer p6bfghn0rsdfghc-34ggb5tdas8w7ysdftj-C4"
 "Content-Type" = "application/json"
 } </code></pre>



<p>Result with success = True shows that the key is valid and accepted.</p>



<figure class="wp-block-image size-large"><img data-recalc-dims="1" decoding="async" src="https://i0.wp.com/tekcookie.com/wp-content/uploads/2021/02/image-6.png?w=1080&#038;ssl=1" alt="" class="wp-image-1912"/></figure>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<p>To know your public IP,  please refer to my other post</p>



<figure class="wp-block-embed is-type-wp-embed is-provider-tekcookie wp-block-embed-tekcookie"><div class="wp-block-embed__wrapper">
<blockquote class="wp-embedded-content" data-secret="JkZMdlu1S7"><a href="https://tekcookie.com/get-your-public-ip-using-powershell/">Get your Public IP using PowerShell</a></blockquote><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  title="&#8220;Get your Public IP using PowerShell&#8221; &#8212; TekCookie" src="https://tekcookie.com/get-your-public-ip-using-powershell/embed/#?secret=JkZMdlu1S7" data-secret="JkZMdlu1S7" width="600" height="338" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>
</div></figure>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">Adding a new record to Cloudflare DNS</h2>



<pre class="wp-block-code"><code lang="powershell" class="language-powershell"> $token = "p6bfghn0rsdfghc-34ggb5tdas8w7ysdftj-C4"
 $hostname = "dyDNS.domain.com"
 $ip = Invoke-RestMethod -uri "https://ifconfig.io/ip"  #Your Public IP 
 $zoneid = "6cd345qefad7c71dfg5q23573017"
 $url = "https://api.clouflare.com/client/v4/zones/$zoneid/dns_records"

 $Body = @{
     "type" = "A"
     "name" =  $hostname
     "content" = $ip
     "proxied" = $true # To mask the real IP
 }

 $Body = $Body | ConvertTo-Json

 $result = Invoke-RestMethod -Method post -Uri $url -Headers @{
 "Authorization" = "Bearer p6bfghn0rsdfghc-34ggb5tasdas8w7ysdftj-C4"
 } -Body $Body -ContentType "application/json"

 $result.result</code></pre>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">Edit an existing Cloudflare DNS record</h2>



<pre class="wp-block-code"><code lang="powershell" class="language-powershell"> $hostname = "dyDNS.domain.com"
 $zoneid = "6cd345qefad7c71dfg5q23573017"
 $token = "p6bfghn0rsdfghc-34ggb5tdas8w7ysdftj-C4"
 $url = "https://api.cloudflare.com/client/v4/zones/$zoneid/dns_records" 

 # Fetch the record information
 $record_data = Invoke-RestMethod -Method get -Uri "$url/?name=$hostname" -Headers @{
 "Authorization" = "Bearer $token"
 } 

 # Modify the IP from the fetched record
 $record_ID = $record_data.result[0].id
 $record_data.result[0].content = Invoke-RestMethod -uri "https://ifconfig.io/ip" #Your Public IP 

 $body = $record_data.result[0] | ConvertTo-Json

 # Update the record
 $result = Invoke-RestMethod -Method put -Uri "$url/$record_ID" -Headers @{
 "Authorization" = "Bearer $token"
 } -Body $body -ContentType "application/json"</code></pre>



<p>Run this script in a Scheduled Task, your Public IP will get updated in DNS !!!</p>



<div style="height:30px" aria-hidden="true" class="wp-block-spacer"></div>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>Hope you like this article and thank you for reading.</p></blockquote>



<p></p>
]]></content:encoded>
					
					<wfw:commentRss>https://tekcookie.com/dynamic-dns-with-cloudflare-and-powershell/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1900</post-id>	</item>
		<item>
		<title>Get your Public IP using PowerShell</title>
		<link>https://tekcookie.com/get-your-public-ip-using-powershell/</link>
					<comments>https://tekcookie.com/get-your-public-ip-using-powershell/#respond</comments>
		
		<dc:creator><![CDATA[jeffythampi]]></dc:creator>
		<pubDate>Sat, 13 Feb 2021 15:38:33 +0000</pubDate>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Public IP]]></category>
		<category><![CDATA[Public IP using PowerShell]]></category>
		<guid isPermaLink="false">https://tekcookie.com/?p=1916</guid>

					<description><![CDATA[The devices behind a modem or an internet router will be assigned with private ip for communication within the internal network. When these devices needed to communicate to the outside world, the modem/router translate the private IP address to the public IP address which is called as NAT ( Network address translation ) To get [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>The devices behind a modem or an internet router will be assigned with private ip for communication within the internal network. When these devices needed to communicate to the outside world, the modem/router translate the private IP address to the public IP address which is called as NAT ( Network address translation )</p>



<p>To get information about our public IP, open any web browser and visit the page <a href="https://ifconfig.io/" target="_blank" rel="noreferrer noopener nofollow">https://ifconfig.io/</a>.</p>



<p>The same page also provide api to get the details.</p>



<h3 class="wp-block-heading">Public IP using PowerShell</h3>



<div class="wp-block-columns are-vertically-aligned-center is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-vertically-aligned-center is-layout-flow wp-block-column-is-layout-flow" style="flex-basis:100%">
<div class="wp-block-group"><div class="wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow">
<div class="wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<pre class="wp-block-code"><code lang="powershell" class="language-powershell">( Invoke-WebRequest -uri "https://ifconfig.io/ip" ).content </code></pre>
</div>
</div>
</div></div>
</div>
</div>



<p>Refer to <a href="https://ifconfig.io/" target="_blank" rel="noreferrer noopener">https://ifconfig.io/</a> to get more information about the API</p>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<blockquote class="wp-block-quote is-style-default is-layout-flow wp-block-quote-is-layout-flow"><p>Hope you like this article and thank you for reading.</p></blockquote>
]]></content:encoded>
					
					<wfw:commentRss>https://tekcookie.com/get-your-public-ip-using-powershell/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1916</post-id>	</item>
		<item>
		<title>Install drivers in Windows using PowerShell</title>
		<link>https://tekcookie.com/auto-install-drivers-using-powershell/</link>
					<comments>https://tekcookie.com/auto-install-drivers-using-powershell/#respond</comments>
		
		<dc:creator><![CDATA[jeffythampi]]></dc:creator>
		<pubDate>Wed, 16 Dec 2020 18:22:09 +0000</pubDate>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[Windows 10]]></category>
		<category><![CDATA[Windows Server 2016]]></category>
		<category><![CDATA[Install drivers with PowerShell]]></category>
		<guid isPermaLink="false">https://tekcookie.com/?p=1811</guid>

					<description><![CDATA[Installing drivers for windows is very time consuming when the driver package contain multiple devices/models files with all possible platform architecture(x86, x64, etc.). We can leverage the pnputil.exe tool to perform the installation fast and easy. Consider a scenario of a multiple drivers packed to an iso image. Below script will install all the required [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>Installing drivers for windows is very time consuming when the driver package contain multiple devices/models files with all possible platform architecture(x86, x64, etc.).</p>



<p>We can leverage the pnputil.exe tool to perform the installation fast and easy.</p>



<p>Consider a scenario of a multiple drivers packed to an iso image. Below script will install all the required drivers.</p>



<pre class="wp-block-code"><code lang="powershell" class="language-powershell line-numbers"># Mount the driver iso image
Mount-DiskImage D:\Driver\drivers-windows.iso

# Get the mount point/drive letter, considering that the above one is the only disk mounted
$isoMount = (Get-DiskImage -DevicePath \\.\CDROM0  | Get-Volume).DriveLetter

# Find the inf files and install
Get-ChildItem "$($isoMount):\" -Recurse -Include *.inf | ForEach-Object {
     $_.FullName
     pnputil /add-driver $_.FullName /install 
}</code></pre>



<p>Running the script would update all the drivers which are meant for your system hardware.</p>



<div style="height:70px" aria-hidden="true" class="wp-block-spacer"></div>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>Hope you like this article and thank you for reading.</p></blockquote>
]]></content:encoded>
					
					<wfw:commentRss>https://tekcookie.com/auto-install-drivers-using-powershell/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1811</post-id>	</item>
		<item>
		<title>Execute PowerShell Remotely with PSEXEC</title>
		<link>https://tekcookie.com/execute-powershell-remotely-with-psexec/</link>
					<comments>https://tekcookie.com/execute-powershell-remotely-with-psexec/#respond</comments>
		
		<dc:creator><![CDATA[jeffythampi]]></dc:creator>
		<pubDate>Mon, 19 Oct 2020 13:37:52 +0000</pubDate>
				<category><![CDATA[CMD]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[connec to remote pc using psexec]]></category>
		<category><![CDATA[powershell-remoting]]></category>
		<category><![CDATA[psexec]]></category>
		<guid isPermaLink="false">https://tekcookie.com/?p=1742</guid>

					<description><![CDATA[PowerShell commands can be executed on a remote systems by using PowerShell-Remoting. By default, PowerShell remoting is disabled on clients operating systems. We can either enable PowerShell remoting by executing the command &#8220;Enable-PSRemoting -Force&#8221; under administrative context. This can be either done locally on each systems by executing the commands or remotely via a group [&#8230;]]]></description>
										<content:encoded><![CDATA[				<div class="wp-block-uagb-table-of-contents uagb-toc__align-left uagb-toc__columns-1  uagb-block-4f7ab2dc     "
					data-scroll= "1"
					data-offset= "30"
					style=""
				>
				<div class="uagb-toc__wrap">
						<div class="uagb-toc__title">
							Table Of Contents						</div>
																<div class="uagb-toc__list-wrap">
						<ol class="uagb-toc__list"><li class="uagb-toc__list"><a href="#interactive-powershell-prompt-with-psexec" class="uagb-toc-link__trigger">Interactive Powershell prompt with PSExec</a><li class="uagb-toc__list"><a href="#execute-multiple-powershell-commands-with-psexec" class="uagb-toc-link__trigger">Execute multiple Powershell commands with PSExec</a><li class="uagb-toc__list"><a href="#execute-multiple-powershell-commands-with-psexec" class="uagb-toc-link__trigger">Execute multiple Powershell commands with PSExec</a></ol>					</div>
									</div>
				</div>
			


<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<p>PowerShell commands can be executed on a remote systems by using PowerShell-Remoting. By default, PowerShell remoting is disabled on clients operating systems.</p>



<p>We can either enable PowerShell remoting by executing the command &#8220;Enable-PSRemoting -Force&#8221; under administrative context. This can be either done locally on each systems by executing the commands or remotely via a group policy or through SCCM.</p>



<p>But if we have administrative access to a system, we can execute PowerShell commands remotely by means of PSEXEC.EXE (https://docs.microsoft.com/en-us/sysinternals/downloads/psexec)</p>



<p>PSEXEC shell does not work well (interactively) with PowerShell as it works with cmd commands.</p>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">Interactive Powershell prompt with PSExec </h2>



<p>In windows 10, the interactive part is much better.</p>



<p>Enabling PowerShell Remoting using PSEXEC</p>



<pre class="wp-block-code"><code lang="bash" class="language-bash">#cmd.exe in the administrative context
#navigate to the sysinternals tools folder or add the folder to the PATH
#executing the below command would take us to the powershell prompt of the remote system
psexec.exe \\&lt;computerName powershell.exe

#run the below command to enable PS Remoting or any commands as we wish
Enable-PSRemoting -Force</code></pre>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading"> Execute multiple Powershell commands with PSExec </h2>



<p>So, rather than going with interactive way, we can also issue a single command to do the same task</p>



<pre class="wp-block-code"><code lang="bash" class="language-bash">#navigate to the sysinternals suite folder, to directly execute from the prompt, add the folder to the PATH
psexec.exe \\&lt;computerName&gt; powershell.exe -command "&amp; {Enable-PSRemoting -Force}"</code></pre>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">Execute multiple Powershell commands with PSExec</h2>



<p>We can also execute multiple commands as a single statement</p>



<pre class="wp-block-code"><code lang="bash" class="language-bash">#navigate to the sysinternals suite folder, to directly execute from the prompt, add the folder to the PATH
psexec.exe \\&lt;computerName&gt; powershell.exe -command "&amp; {Get-Process; Get-service; $num1 = 2; $num2 = 1; $num1 + $num2}"</code></pre>



<div style="height:30px" aria-hidden="true" class="wp-block-spacer"></div>



<p>Also, please have a look at my other post about PSEXEC</p>



<figure class="wp-block-embed is-type-wp-embed is-provider-tekcookie wp-block-embed-tekcookie"><div class="wp-block-embed__wrapper">
<blockquote class="wp-embedded-content" data-secret="3LKc5DQQbg"><a href="https://tekcookie.com/control-remote-windows-system-with-command-line-psexec/">Control remote windows system with command line &#8211; PSEXEC</a></blockquote><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  title="&#8220;Control remote windows system with command line &#8211; PSEXEC&#8221; &#8212; TekCookie" src="https://tekcookie.com/control-remote-windows-system-with-command-line-psexec/embed/#?secret=3LKc5DQQbg" data-secret="3LKc5DQQbg" width="600" height="338" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>
</div></figure>



<div style="height:30px" aria-hidden="true" class="wp-block-spacer"></div>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>Hope you liked this article and thank you for reading</p></blockquote>



<p>&nbsp;</p>
]]></content:encoded>
					
					<wfw:commentRss>https://tekcookie.com/execute-powershell-remotely-with-psexec/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1742</post-id>	</item>
		<item>
		<title>Device driver information using PowerShell</title>
		<link>https://tekcookie.com/device-driver-information-using-powershell/</link>
					<comments>https://tekcookie.com/device-driver-information-using-powershell/#respond</comments>
		
		<dc:creator><![CDATA[jeffythampi]]></dc:creator>
		<pubDate>Tue, 01 Sep 2020 02:34:06 +0000</pubDate>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[Device Driver]]></category>
		<category><![CDATA[Driver Info PowerShell]]></category>
		<guid isPermaLink="false">https://tekcookie.com/?p=1575</guid>

					<description><![CDATA[In this article, we will see how to get windows device driver details using PowerShell. Finding driver information is necessary as information gathering prior to software or system upgrades. WMI class Win32_PnPSignedDriver is used to get the device driver information This would enumerate complete information in the local system. To get information from multiple systems, [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p></p>



<p>In this article, we will see how to get windows device driver details using PowerShell. Finding driver information is necessary as information gathering prior to software or system upgrades.</p>



<figure class="wp-block-image size-large is-resized"><img data-recalc-dims="1" decoding="async" src="https://i0.wp.com/tekcookie.com/wp-content/uploads/2020/08/DeviceDriver.jpg?resize=337%2C383&#038;ssl=1" alt="" class="wp-image-2786" width="337" height="383" srcset="https://i0.wp.com/tekcookie.com/wp-content/uploads/2020/08/DeviceDriver.jpg?w=403&amp;ssl=1 403w, https://i0.wp.com/tekcookie.com/wp-content/uploads/2020/08/DeviceDriver.jpg?resize=264%2C300&amp;ssl=1 264w" sizes="(max-width: 337px) 100vw, 337px" /></figure>



<p>WMI class Win32_PnPSignedDriver is used to get the device driver information</p>



<pre class="wp-block-code"><code lang="powershell" class="language-powershell">#Fetch all properties
Get-CimObject Win32_PnPSignedDriver | fl *</code></pre>



<pre class="wp-block-code"><code lang="powershell" class="language-powershell">Get-CimInstance Win32_PnPSignedDriver | `
    Select DeviceClass, DeviceID, DeviceName, InfName, DriverVersion, FriendlyName, IsSigned</code></pre>



<p>This would enumerate complete information in the local system.</p>



<p>To get information from multiple systems, the command has to be executed against the respective servers. The server names can be either fetched from active directory or from text file.</p>



<p>The code below will query for IBM SDDDSM driver information from a list of servers in a text file.</p>



<pre class="wp-block-code"><code lang="powershell" class="language-powershell line-numbers">$servers = Get-Content "D:\server.txt"

foreach($server in $servers)
{
  
    Get-CimInstance Win32_PnPSignedDriver -ComputerName $server -ErrorAction SilentlyContinue | `
        where {$_.devicename -like "*IBM SDDDSM*"} | `
        select PSComputerName, DeviceClass, DeviceID, DeviceName, InfName, DriverVersion, FriendlyName, IsSigned
    if($Error)
    {
        #Display error if any server fails
        $server + " Error"
        $Error.Clear()
    }
}</code></pre>



<div style="height:70px" aria-hidden="true" class="wp-block-spacer"></div>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>Hope this is helpful and thank you for reading.</p></blockquote>
]]></content:encoded>
					
					<wfw:commentRss>https://tekcookie.com/device-driver-information-using-powershell/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1575</post-id>	</item>
		<item>
		<title>Use DLL files in PowerShell</title>
		<link>https://tekcookie.com/use-dll-files-in-powershell/</link>
					<comments>https://tekcookie.com/use-dll-files-in-powershell/#comments</comments>
		
		<dc:creator><![CDATA[jeffythampi]]></dc:creator>
		<pubDate>Sat, 15 Aug 2020 21:16:51 +0000</pubDate>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[.Net in PowerShell]]></category>
		<category><![CDATA[dll in PowerShell]]></category>
		<guid isPermaLink="false">https://tekcookie.com/?p=1490</guid>

					<description><![CDATA[In the previous, we saw how to embed C# code in the PowerShell script. This article is about accessing the dll assembly files in PowerShell Call dll assembly in PowerShell Suppose the below C# code is compiled to a dll file custlib.dll, we can add the assembly to PowerShell. Accessing dll in PowerShell Load assembly [&#8230;]]]></description>
										<content:encoded><![CDATA[				<div class="wp-block-uagb-table-of-contents uagb-toc__align-left uagb-toc__columns-1  uagb-block-53a3d04d     "
					data-scroll= "1"
					data-offset= "30"
					style=""
				>
				<div class="uagb-toc__wrap">
						<div class="uagb-toc__title">
							Table Of Contents						</div>
																<div class="uagb-toc__list-wrap">
						<ol class="uagb-toc__list"><li class="uagb-toc__list"><a href="#call-dll-assembly-in-powershell" class="uagb-toc-link__trigger">Call dll assembly in PowerShell</a><li class="uagb-toc__list"><a href="#accessing-dll-in-powershell" class="uagb-toc-link__trigger">Accessing dll in PowerShell</a><li class="uagb-toc__list"><a href="#compiling-c-files-to-dll" class="uagb-toc-link__trigger">Compiling C# files to dll</a><ul class="uagb-toc__list"><li class="uagb-toc__list"><a href="#one-liner-to-compile-the-c-to-dll-using-powershell" class="uagb-toc-link__trigger">One-liner to compile the C# to dll using PowerShell</a></ul></ol>					</div>
									</div>
				</div>
			


<div style="height:30px" aria-hidden="true" class="wp-block-spacer"></div>



<p>In the previous, we saw how to embed C# code in the PowerShell script. This article is about accessing the dll assembly files in PowerShell</p>



<figure class="wp-block-embed is-type-wp-embed is-provider-tekcookie wp-block-embed-tekcookie"><div class="wp-block-embed__wrapper">
<blockquote class="wp-embedded-content" data-secret="yI6v9iKfDh"><a href="https://tekcookie.com/use-c-in-powershell/">Use C# in PowerShell</a></blockquote><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  title="&#8220;Use C# in PowerShell&#8221; &#8212; TekCookie" src="https://tekcookie.com/use-c-in-powershell/embed/#?secret=yI6v9iKfDh" data-secret="yI6v9iKfDh" width="600" height="338" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>
</div></figure>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">Call dll assembly in PowerShell</h2>



<p>Suppose the below C# code is compiled to a dll file <em>custlib.dll</em>, we can add the assembly to PowerShell.</p>



<pre class="wp-block-code"><code lang="csharp" class="language-csharp line-numbers">using System;
namespace CustomNamespace
{
    public class CustomClass
    {
        //Function to Split the string
        public string[] Split(string value, char delimiter)
        {
            string[] strArray = value.Split(delimiter);
            return strArray;
        }

        //Function to sort
        public string[] Sorts(string[] strArray)
        {
            Array.Sort(strArray);
            return strArray;
        }
    }
}</code></pre>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">Accessing dll in PowerShell</h2>



<p>Load assembly Using <strong>Add-Type</strong> cmdlet</p>



<pre class="wp-block-code"><code lang="powershell" class="language-powershell line-numbers">Add-Type -Path "C:\PowerShell\custlib.dll"

#Create object of CustomClass
$custObj = New-Object CustomNamespace.CustomClass

#Calling Split function
$custObj.Split("one,two,three",",")

#Calling Sort function
$custObj.Sorts(@("c","a","b"))</code></pre>



<p>Load assembly using .Net Reflection class Load method</p>



<pre class="wp-block-code"><code lang="powershell" class="language-powershell line-numbers">$AssemblyPath = "C:\PowerShell\custlib.dll"

#The dll file has to read as byte string
#The below cmdlet Get-Content with parameter -AsByteStream will only work in PowerShell version 6 and above.
#So .Net class System.IO.File can be used to read the file as byte stream
#$byteValues = Get-Content $AssemblyPath -AsByteStream -Raw

$byteValues = [System.IO.File]::ReadAllBytes($AssemblyPath)
[System.Reflection.Assembly]::Load($byteValues)


#Create object of CustomClass
$custObj = New-Object CustomNamespace.CustomClass

#Calling Split function
$custObj.Split("one,two,three",",")

#Calling Sort function
$custObj.Sorts(@("c","a","b"))</code></pre>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">Compiling C# files to dll</h2>



<p>CSharp code can be compiled to dll files using csc command line compiler.</p>



<h4 class="wp-block-heading">One-liner to compile the C# to dll using PowerShell</h4>



<p>Save the C# source code to custlib.cs to any folder</p>



<pre class="wp-block-code"><code lang="powershell" class="language-powershell">&amp;"$env:windir\Microsoft.NET\Framework\v4.0.30319/csc" /target:library C:\PowerShell\custlib.cs </code></pre>



<p>Running the above line will generate the dll file.</p>



<p>Reference taken from <a href="https://gallery.technet.microsoft.com/scriptcenter/c66c2a00-8a24-4412-aa00-c18bda570508" target="_blank" rel="noopener">https://gallery.technet.microsoft.com/scriptcenter/c66c2a00-8a24-4412-aa00-c18bda570508</a></p>



<div style="height:70px" aria-hidden="true" class="wp-block-spacer"></div>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>Hope this is helpful and thank you for reading.</p></blockquote>
]]></content:encoded>
					
					<wfw:commentRss>https://tekcookie.com/use-dll-files-in-powershell/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1490</post-id>	</item>
		<item>
		<title>Use C# in PowerShell</title>
		<link>https://tekcookie.com/use-c-in-powershell/</link>
					<comments>https://tekcookie.com/use-c-in-powershell/#respond</comments>
		
		<dc:creator><![CDATA[jeffythampi]]></dc:creator>
		<pubDate>Sat, 15 Aug 2020 08:47:11 +0000</pubDate>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[C# in PowerShell]]></category>
		<guid isPermaLink="false">https://tekcookie.com/?p=1481</guid>

					<description><![CDATA[PowerShell is a cross-platform language build on top of .Net framework which accepts and returns objects. PowerShell can access the libraries written in C#, VB, jScript. Scripts are usually build using PowerShell cmdlets but we can use the libraries coded in C#. Embed C# (CSharp) code directly in PowerShell Below example shows how to use [&#8230;]]]></description>
										<content:encoded><![CDATA[				<div class="wp-block-uagb-table-of-contents uagb-toc__align-left uagb-toc__columns-1  uagb-block-1fcddde2     "
					data-scroll= "1"
					data-offset= "30"
					style=""
				>
				<div class="uagb-toc__wrap">
						<div class="uagb-toc__title">
							Table Of Contents						</div>
																<div class="uagb-toc__list-wrap">
						<ol class="uagb-toc__list"><li class="uagb-toc__list"><a href="#embed-c-csharp-code-directly-in-powershell" class="uagb-toc-link__trigger">Embed C# (CSharp) code directly in PowerShell</a><li class="uagb-toc__list"><a href="#benefits-of-using-c-csharp-in-powershell" class="uagb-toc-link__trigger">Benefits of using C# (CSharp) in PowerShell</a></ol>					</div>
									</div>
				</div>
			


<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<p>PowerShell is a cross-platform language build on top of .Net framework which accepts and returns objects. PowerShell can access the libraries written in C#, VB, jScript.</p>



<p>Scripts are usually build using PowerShell cmdlets but we can use the libraries coded in C#.</p>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">Embed C# (CSharp) code directly in PowerShell</h2>



<p>Below example shows how to use C# (CSharp) code directly in PowerShell</p>



<pre class="wp-block-code"><code lang="powershell" class="language-powershell line-numbers">#Defining Class in C# with functionality methods

$Source = @"
using System;
namespace CustomNamespace
{
    public class CustomClass
    {
        //Function to Split the string
        public string[] Split(string value, char delimiter)
        {
            string[] strArray = value.Split(delimiter);
            return strArray;
        }

        //Function to sort
        public string[] Sorts(string[] strArray)
        {
            Array.Sort(strArray);
            return strArray;
        }
    }
}
"@

#Adding the C# source code to powershell    
Add-Type -TypeDefinition $Source -Language CSharp

#Creating the object of the class defined in C#
$objCustomClass = New-Object CustomNamespace.CustomClass

#Sorting array of string
"Sorting array of string`n" | Out-Host
$arrays = "b","a","c"
"Original array" | Out-Host
$arrays

#Sorting using C# custom method
"Sorted array using C#" | Out-Host
$objCustomClass.Sorts($arrays)

#Sorting using PowerShell by using dotnet class
"Sorted array using PowerShell" | Out-Host
[Array]::Sort($arrays)
$arrays


#Split string
"`n`nSplit function`n" | Out-Host
$str = "one,two,three"
$delimiter = ","
"Source string is : " + $str + " `nand delimiter is : " + $delimiter + "`n"| Out-Host
#Split using C# custom method
"Split string using C#" | Out-Host
$objCustomClass.Split($str, $delimiter)

#Split using powershell
"Split string using PowerShell" | Out-Host
$str.Split($delimiter)

</code></pre>



<p>Result</p>



<figure class="wp-block-image size-large"><img data-recalc-dims="1" decoding="async" src="https://i0.wp.com/tekcookie.com/wp-content/uploads/2020/08/CSharp_Out_1.jpg?w=1080&#038;ssl=1" alt="" class="wp-image-1486"/></figure>



<h2 class="wp-block-heading">Benefits of using C# (CSharp) in PowerShell</h2>



<ol class="wp-block-list"><li>.Net codes are faster than PowerShell scripts</li><li>Can use the existing libraries coded in C# or vb</li></ol>



<div style="height:70px" aria-hidden="true" class="wp-block-spacer"></div>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>Hope this is helpful and thank you for reading.</p></blockquote>



<p>&nbsp;</p>
]]></content:encoded>
					
					<wfw:commentRss>https://tekcookie.com/use-c-in-powershell/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1481</post-id>	</item>
		<item>
		<title>Create NSG in Azure using PowerShell</title>
		<link>https://tekcookie.com/create-nsg-in-azure-using-powershell/</link>
					<comments>https://tekcookie.com/create-nsg-in-azure-using-powershell/#respond</comments>
		
		<dc:creator><![CDATA[jeffythampi]]></dc:creator>
		<pubDate>Fri, 14 Aug 2020 13:12:01 +0000</pubDate>
				<category><![CDATA[Azure]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Azure Virtual Network]]></category>
		<category><![CDATA[NSG]]></category>
		<category><![CDATA[Subnet]]></category>
		<category><![CDATA[Virtual Network]]></category>
		<guid isPermaLink="false">https://tekcookie.com/?p=1471</guid>

					<description><![CDATA[This article is about how to create a Network Security Group in Azure Cloud. NSG (a very basic firewall) is a service provided by Azure cloud to manage the traffic flow in Azure virtual network and resources. The traffic management is done by access control lists (ACL). The security rules defines the protocol (TCP, UDP, [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>This article is about how to create a Network Security Group in Azure Cloud. NSG (a very basic firewall) is a service provided by Azure cloud to manage the traffic flow in Azure virtual network and resources. <br>The traffic management is done by access control lists (ACL). The security rules defines the protocol (TCP, UDP, ICMP), source, source port, destination, destination port, allow/deny and priority. NSG can be attached to the subnet or virtual machines NIC and the security rules are applied to the whole subnet or the virtual machines accordingly.</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img data-recalc-dims="1" decoding="async" src="https://i0.wp.com/tekcookie.com/wp-content/uploads/2020/08/NSG-1.jpg?w=1080&#038;ssl=1" alt="" class="wp-image-1475"/></figure></div>



<p>NSG is stateful, which means if an incoming port is open and when a connection hits the port, outgoing port is automatically opened to allow the return traffic.</p>



<p>In the previous post, we saw how to create virtual network and subnets in Azure. </p>



<figure class="wp-block-embed is-type-wp-embed is-provider-tekcookie wp-block-embed-tekcookie"><div class="wp-block-embed__wrapper">
<blockquote class="wp-embedded-content" data-secret="pLPppd1iib"><a href="https://tekcookie.com/create-virtual-network-in-azure-using-powershell/">Create Virtual Network in Azure using PowerShell</a></blockquote><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  title="&#8220;Create Virtual Network in Azure using PowerShell&#8221; &#8212; TekCookie" src="https://tekcookie.com/create-virtual-network-in-azure-using-powershell/embed/#?secret=pLPppd1iib" data-secret="pLPppd1iib" width="600" height="338" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>
</div></figure>



<p><strong>Existing infrastructure diagram (refer the above mentioned article) </strong></p>



<div class="wp-block-image"><figure class="aligncenter size-large is-resized"><img data-recalc-dims="1" decoding="async" src="https://i0.wp.com/tekcookie.com/wp-content/uploads/2020/08/Network-basic-vnetsubnetonly.jpg?resize=343%2C210&#038;ssl=1" alt="" class="wp-image-1476" width="343" height="210"/><figcaption>Virtual network and Subnets</figcaption></figure></div>



<p>This article will describe how to create two network security group for the internal and DMZ subnet.</p>



<p>For the subnets, two NSG is required to manage the traffic.</p>



<pre title="#Create NSG in Azure VNet and attach to a Subnet" class="wp-block-code"><code lang="powershell" class="language-powershell line-numbers">
#Connect to azure network
Connect-AzAccount 

#virtual network information
$resource_locaton_main = 'West US'
$resourceGroup_Name = "RG-HOSite"
$vnet_Name = "vnet-ho"

#Fetching information about resource group and virtual network
$rg = Get-AzResourceGroup $resourceGroup_Name
$vnet = Get-AzVirtualNetwork -Name $vnet_Name -ResourceGroupName $rg.ResourceGroupName

#Getting subnet information from the virtual network object
$subnet_INT = $vnet.Subnets[0]
$subnet_DMZ = $vnet.Subnets[1]


#Creating internal network NSG
$nsg_int = New-AzNetworkSecurityGroup -Name "nsg-int" `
-Location $rg.Location `
-ResourceGroupName $rg.ResourceGroupName

#Adding the nsg to VNET INT
Set-AzVirtualNetworkSubnetConfig -NetworkSecurityGroup $nsg_int `
-Name $subnet_INT.Name `
-VirtualNetwork $vnet `
-AddressPrefix $subnet_INT.AddressPrefix | Set-AzVirtualNetwork

#Creating DMZ network NSG
$nsg_dmz = New-AzNetworkSecurityGroup -Name "nsg-dmz" `
-Location $vnet.Location `
-ResourceGroupName $rg.ResourceGroupName

#adding the DMZ NSG to subnet
Set-AzVirtualNetworkSubnetConfig -Name $subnet_DMZ.Name  `
-VirtualNetwork $vnet  `
-NetworkSecurityGroup $nsg_dmz `
-AddressPrefix $subnet_DMZ.AddressPrefix | Set-AzVirtualNetwork</code></pre>



<p>Executing the above script will create two NSG and attach to the internal and DMZ subnet as shown below.</p>



<figure class="wp-block-image size-large is-resized"><img data-recalc-dims="1" decoding="async" src="https://i0.wp.com/tekcookie.com/wp-content/uploads/2020/08/vnet-subnet-dmz.jpg?resize=368%2C339&#038;ssl=1" alt="" class="wp-image-1477" width="368" height="339"/></figure>



<div style="height:70px" aria-hidden="true" class="wp-block-spacer"></div>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>Hope this is informative and thank you for reading.</p></blockquote>
]]></content:encoded>
					
					<wfw:commentRss>https://tekcookie.com/create-nsg-in-azure-using-powershell/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1471</post-id>	</item>
	</channel>
</rss>
