User Profile
SBVRaja
Copper Contributor
Joined 6 years ago
User Widgets
Recent Discussions
Alternative to invoke-expression
I am looking for alternate way to extract the value of the below powershell object without using invoke-expression invoke-expression "`$abc.properties.$subprop" or invoke-expression "`$abc.$subprop1" Where $abc is a powershell object in JSON format $subprop is having one of the Noteproperties of '$abc.properties'Re: powershell to run git commit using PAT in Azure github pipelines
Solution: If you are still looking for a solution, here it is. If you found any alternate solution please feel free to post. #To identify branch name in which the build pipeline is running if($env:SYSTEM_PULLREQUEST_SOURCEBRANCH) { "This is a PR build" $branch = ("$env:SYSTEM_PULLREQUEST_SOURCEBRANCH").replace("refs/heads/","") Write-Host "Branch name is: " $branch } elseif($env:BUILD_SOURCEBRANCH) { "This is a Non-PR build pipeline run" $branch = ("$env:BUILD_SOURCEBRANCH").replace("refs/heads/","") } Else { Exit } git checkout $branch git pull <#Your code & changes to commig#> git add . git config --global user.email "Any email id" git config --global user.name "Any user name" git commit -am "commit message [ci skip]" #To skip the automatic trigger of the build use [ci skip] git push --set-upstream origin $branch Note: You need to enable the option "Allow scripts to access the OAuth token" on the 'Agent job' of the pipeline under Additional Options5.6KViews0likes0CommentsRe: powershell to run git commit using PAT in Azure github pipelines
If you are still looking for a solution, here it is. If you found any alternate solution please feel free to post. #To identify branch name in which the build pipeline is running if($env:SYSTEM_PULLREQUEST_SOURCEBRANCH) { "This is a PR build" $branch = ("$env:SYSTEM_PULLREQUEST_SOURCEBRANCH").replace("refs/heads/","") Write-Host "Branch name is: " $branch } elseif($env:BUILD_SOURCEBRANCH) { "This is a Non-PR build pipeline run" $branch = ("$env:BUILD_SOURCEBRANCH").replace("refs/heads/","") } Else { Exit } git checkout $branch git pull <#Your code & changes to commig#> git add . git config --global user.email "Any email id" git config --global user.name "Any user name" git commit -am "commit message [ci skip]" #To skip the automatic trigger of the build use [ci skip] git push --set-upstream origin $branch Note: You need to enable the option "Allow scripts to access the OAuth token" on the 'Agent job' of the pipeline under Additional Options5.5KViews0likes0Commentspowershell to run git commit using PAT in Azure github pipelines
I created a Personal Access Token for my repository and named it as 'PAT', created a variable called 'PATSecret' in my pipeline for the secret of PAT. Passing PATSecret as an input variable for $Cred. But when trying to use this secret to run commit command, authentication is failing. param( [string]$Cred ) git --version git add . Write-Host "setup author info" git config user.email you@you.com git config user.name "your name" Write-Host "git commit with message" git commit -m "Test Commit from Azure DevOps" git push -u https://PAT:$($cred)@dev.azure.com/project/_git/myrepo HEAD Error message: fatal: Authentication failed for 'https://dev.azure.com/project/_git/myrepo/' ##[error]PowerShell exited with code '1'.SolvedRe: Return command is not coming out of function and not returning value
JRCigars Thanks for looking into this and your time JRCigars. I really appreciate that. My actual problem here is not to find alternate solutions (As mentioned, I already had an alternate solution and I also believe there are many other solutions too just like Do-While you used which is also a great approach which I didn't think). But my actual cause here is to understand the behavior of 'Return' and put a '?' at where ever I don't have a proper answer Considering this, here are my responses to your 7 points with '?' .. 🙂 1) Using recursive calls to CFile is probably not the best approach, especially from inside the catch {} block. Raja: Understood. I may agree with you on this. But WHY? 2) The $Done variable is never properly initialized, and the only possible return value is "Good job" for $Required. Raja: I don't think it is a big issue (in fact not a issue at all). In fact, the code that i gave is very simplified version of actual bulk code. These $Done, $title, the function CFile itself are all its simplified versions. 3) I might recommend moving any statements referencing $JSONformat inside your try {} block Raja: I completely agree. This is actually simplifying the issue completely. 4) Your $title argument to the CFile function is never used. Raja: I don't think its of big issue (in fact not a issue at all). In actual code, I generally use this variable to display while reading $filepath in try{} block as this function 'Cfile' i use for multi purpose. In fact, the code that i gave is very simplified version of actual bulk code. These $Done, $title, the function CFile itself are all its simplified versions. 5) If possible, avoid using Write-Host; and instead try using either Write-Verbose, Write-Output, Write-Information, Write-Warning, etc. Raja: I would consider it. I will study this alternate approach 6) Since your Switch() statement only handles 2 conditions, an if() {} else {} block might be cleaner Raja: I agree. But in actual code, there are 3 more conditions that it actually verifies. 7) Return($Done) is mostly equivalent to: $Done; Return; Raja: I agree. But with the use of Return why the engine is not moving out of function??1.4KViews0likes1CommentReturn command is not coming out of function and not returning value
I am using below code 1) To verify whether given input text is in JSON format then 2) User acceptance on the file selected as per its version. Means, if i give wrong JSON format in the input file name, it will ask for a correct formatted file so that I will verify the JSON code in file, correct it and then input again. Then it will verify the version of the JSON file and asks for user acceptance. Post user acceptance, return value otherwise ask for alternate file with required version. But, lets say if i took 'n' iterations to get correct JSON format, post giving correct formatted JSON as input, user acceptance is also iterating 'n' times even though it is accepted initial time itself. Means, 'return' is not coming out of the function (which should actually come out of the function as per my knowledge). Please correct me if my assumption about 'return' is wrong or correct the code. Note: By using another function for JSON validation I got the issue fixed but I don't want to follow that process. Code: function Cfile { param([String]$title) try { $filepath = Read-host "Enter the path of the file" $JSONformat = gc $filepath | ConvertFrom-Json -ErrorAction Stop; Write-Host "Valid JSON format" -ForegroundColor Green } catch { Write-Host "Provided text is not a valid JSON string" -ForegroundColor Red Cfile -title "Select Correct Config file" } Write-Host "Entered baseline version is" $JSONformat.version $confirm = Read-Host "Is that correct?? (Y/N)" Switch($confirm) { Y{ $done = "Good job" Break } Default{ Write-Host "Select Correct Config file" cfile -title "Select Correct Config file" } } Return($Done) } $Required = Cfile "Select required JSON" Input JSON file (correct format): { "version": 87, "TestingJSON": [ { "type": "string", "category": "crytical" } ] } Note: To give wrong JSON format as input, may be you can remove any Comma, brace, quotation .. etc from above file (if you are not much familiar about JSON.Solved1.7KViews0likes3Comments
Recent Blog Articles
No content to show