Skip to main content

Posts

Showing posts with the label powershell

Register/Deregister ADO agents using PowerShell

 here is the script you can use to register/deregister ADO agents PARAM( [int] $NoofAgents, [string] $vsts_url = "https://dev.azure.com/name", [string] $vsts_pat, [string] $svcUserName = "domain\user", [string] $svcPassword, [string] $agentAction ) $agent_pool = 'Poolname'; $ErrorActionPreference="Stop"; If(-NOT (Test-Path $env:SystemDrive\'agents')) {mkdir $env:SystemDrive\'agents'}; Cd $env:SystemDrive\'agents'; if($agentAction -eq "register"){ Write-Host "Download Agent Zip from ADO..." -NoNewline $agentZip="$PWD\agent.zip"; $DefaultProxy=[System.Net.WebRequest]::DefaultWebProxy; $securityProtocol=@(); $securityProtocol+=[Net.ServicePointManager]::SecurityProtocol; $securityProtocol+=[Net.SecurityProtocolType]::Tls12; [Net.ServicePointManager]::SecurityProtocol=$securityProtocol; $WebClient=New-Object N...

Enable Trace on Dynamcis 365 on premise using PowerShell

Enable trace settings through Windows PowerShell Note These changes made in Windows PowerShell do not update the Registry. These changes update the DeploymentProperties and ServerSettingsProperties tables in the MSCRM_CONFIG database. Register the cmdlets 1.      Log in to the administrator account on your Microsoft Dynamics CRM server. 2.      In a Windows PowerShell window, type the following command: Add-PSSnapin Microsoft.Crm.PowerShell To obtain a list of the current settings, type the following command: Get-CrmSetting TraceSettings Set the trace settings 1.      Type the following command: $setting = Get-CrmSetting TraceSettings 2.      Type the following command to enable tracing: $setting.Enabled=$True 3.      Type the following command to set the trace settings: Set-CrmSetting $setting 4.      Type the following command...

Check and Create a Node in XML, if it does not existing using PowerShell

Here is a quick code snippet to Add a Node in XML from from the XPath, the following PowerShell function will search for the node hierarchy, if it does not exist then it will create it. function   CheckForXMLNode  {      param  (          $File , # File Path of XML to search the node          $NodePath     )      $nodes  =  $NodePath  -split  "/"      $path  =  "//Configuration"      # My Sample Node Path is like this # //Configuration/Farm/CustomParent/CustomChild      foreach  ( $node   in   $nodes ) {          if  ( ( $node  -ne  "" ) -and ( $node  -ne  "Configuration" ) ) {   ...

Top 10 Processes using CPU

I came up with following to get CPU% of top 10 process. this can be useful to you as well. $ CpuCores = ( Get-WMIObject Win32_ComputerSystem).NumberOfLogicalProcessors ( Get-Counter "\Process(*)\% Processor Time" ).CounterSamples | Select InstanceName, @ { Name = "CPU %" ; Expression ={[ Decimal ]::Round(( $ _ .CookedValue / $ CpuCores ), 2 )}} | sort *CPU* -Descending | select -First 10

Fixed: Connect CRMOnline with Custom Timeout

I was having trouble with same issue, I was using Connect-CrmOnline method to connect. But I fixed the issue and am able to pass custom timeout value. I am able to import a solution to Dynamics Online which takes around 22 minutes. I hope this will help. Here is the code: #1  Create ConnectionString $cs = "RequireNewInstance=True" $cs+= ";Username=$($Cred.UserName)" $cs+= ";Password=$($Cred.GetNetworkCredential().Password)" $cs+= ";Url=$CRMServerURL" $cs += ";AuthType=Office365" $cs+=";SkipDiscovery=True" #2  Create a connection using ConnectionString (for Online) $Conn = Get-CrmConnection -ConnectionString $cs -MaxCrmConnectionTimeOutMinutes $CrmConTimeOutMinutes #3  Import Solution Import-CrmSolutionAsync -SolutionFilePath $path -conn $Conn -MaxWaitTimeInSeconds $maxWaitTimeInSeconds -ActivateWorkflows -PublishChanges -BlockUntilImportComplete

Quick Trip: Rename Current branch in Git

I love to use Powershell and post-git module for day to day work with git. Sometimes, I have to rename my branches and I have this quick way to do to it. I love to write shortcuts in PowerShell's Profile to save time. Add this function in Powershell Profile.ps1: function RenameLocalBranch { param ( $ NewName ) $ currentBranch = git rev - parse -- abbrev - ref HEAD git branch - m $ NewName git push origin : $ currentBranch $ NewName git push origin - u $ NewName } save the profile.ps1 and open a new go to repo path call the function and give it a new branch name, it should update local and remote branch for you. here is the successful output: C:\Repos\proj [ name1 ≡ ] > RenameLocalBranch name2 Total 0 ( delta 0 ) , reused 0 ( delta 0 ) To https: // xxxxxxxxx.visualstudio.com / _git / xxxxxxxxxx - [ deleted ] name1 * [ new branch ] name2 -> name2 Everything up - to - date Branch ' name2 ...

PowerShell: Get Strong Random Password

function GetRandomPassword { $ strSrc = ' 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$^&(){}[],. ' $ max = $ strSrc.Length $ strPass = '' for ($ i = 0 ; $ i -lt 20 ; $ i += 1 ) { $ index = Get-Random - Minimum 0 - Maximum $ max $ c = $ strSrc [$ index ] $ strPass += $ c } return $ strPass } create the above function in your powershell profile and you can generate random password at anytime. Happy Coding!!

PowerShell: Get Actual Error

I was having hard time to find the reason why I was not able to find a custom method in a .Net DLL. Find your Assembly: PS C:\vstsagent\A1\_work\r1\a\_DevOps_CI\Scripts > [appdomain]::currentdomain . getassemblies() | Where - Object FullName - Match "MyAssembly" GAC Version Location --- ------- -------- False v4 . 0.30319 C:\vstsagent\A1\_work\r1\a\_DevOps_CI\Scripts\Tools\MyAssembly . dll PS C:\vstsagent\A1\_work\r1\a\_DevOps_CI\Scripts & gt; $ a = [appdomain]::currentdomain . getassemblies() | Where - Object FullName - Match "MyAssembly" PS C:\vstsagent\A1\_work\r1\a\_DevOps_CI\Scripts & gt; $ a GAC Version Location --- ------- -------- False v4 . 0.30319 C:\vstsagent\A1\_work\r1\a\_DevOps_CI\Scripts\Tools\MyAssembly . dll When I was trying to get the Types in the assembly, I was getting the exception: PS C:\vstsagent\A1\_work\r1\a\_DevOps_CI\Scripts > ...

Save User Credentials in Secured File

If you want to save credentials in a file and later want to use it in a script here is the trick. You can use  PowerShell's built-in XML serialization (Clixml): #1. Get The Credentials $cred = Get-Credentails #2. Save the Credentails as a file $cred | Export-CliXml "MyFileName.xml" #3. Read the Credentials back from file $myCred = Import-CliXml "<Path>/MyFileName.xml"