Get Workitems from VSTS - tfs

I'm having some troubles to get all workitems/tasks and who is an assignee.
According to this answer is possible get the tasks using the report work, but is retrieving absolutely all.
https://xxx.visualstudio.com/{project}/_apis/wit/reporting/workitemrevisions?includeLatestOnly=true&api-version=5.0-preview.2
Is possible retrieve the id, title and who is an assignee?

You could add parameter fields=System.Id,System.Title,System.AssignedTo in the api:
GET https://{accountName}.visualstudio.com/{project}/_apis/wit/reporting/workitemrevisions?fields=System.Id,System.Title,System.AssignedTo&includeLatestOnly=true&api-version=5.0-preview.2

You can use below PowerShell script to call the REST API and retrieve the work item id, title and assignee and any other elements you needed.
Alternatively you can export the work item list to a *.csv file.
Param(
[string]$collectionurl = "https://xxx.visualstudio.com",
[string]$project = "ProjectName",
[string]$user = "username",
[string]$token = "Password/PAT"
)
# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
$baseUrl = "$collectionurl/$project/_apis/wit/reporting/workitemrevisions?includeLatestOnly=true&api-version=5.0-preview.2"
$response = (Invoke-RestMethod -Uri $baseUrl -Method Get -UseDefaultCredential -Headers #{Authorization=("Basic {0}" -f $base64AuthInfo)}).values
$wits = $response | where({$_.fields.'System.WorkItemType' -eq 'Task'}) # Only retrieve Tasks
$witrevisions = #()
foreach($wit in $wits){
$customObject = new-object PSObject -property #{
"WitID" = $wit.fields.'System.Id'
"rev" = $wit.fields.'System.Rev'
"Title" = $wit.fields.'System.Title'
"AssignedTo" = $wit.fields.'System.AssignedTo'
"ChangedDate" = $wit.fields.'System.ChangedDate'
"ChangedBy" = $wit.fields.'System.ChangedBy'
"WorkItemType" = $wit.fields.'System.WorkItemType'
}
$witrevisions += $customObject
}
$witrevisions | Select-Object `
WitID,
rev,
Title,
AssignedTo,
ChangedDate,
ChangedBy,
WorkItemType #| export-csv -Path D:\temp\WIT.csv -NoTypeInformation

Related

Powershell Parsing CSV file that contains comma

Consider that CSV file:
Node Name,Client Name,Job Directory,Policy Name
server1,test.domain.com,"vmware:/?filter= VMHostName AnyOf "server2.domain.com", "server3.domain.com"",TEST
My code:
$events = Import-Csv "C:\file.csv" | foreach {
New-Object PSObject -prop #{
Server = $_.{Node Name};
Client = $_.{Client Name};
{JobDirectory/Script} = $_.{Job Directory};
Policy = $_.{Policy Name};
}
}
I have some problems when I try to parse the third field. I am not sure if its because the comma, or the double quote.
This is the object I would like to have:
Node Name : server1
Client Name : test.domain.com
JobDirectory/Script : vmware:/?filter= VMHostName AnyOf "server2.domain.com", "server3.domain.com"
Policy Name : TEST
Can someone help me?
Ok, so the easiest way to approach this is to read the file in with Get-Content and then split each line where the commas are not inside quotes. I borrowed the regex from this solution for this.
Using your current input data I would do something like this
$filedata = Get-Content C:\temp\test.csv
$asObject = ForEach($singlerow in ($filedata | Select-Object -Skip 1)){
$props = #{}
$singlerow = $singlerow -split ',(?=(?:[^"]*"[^"]*")*[^"]*$)'
[pscustomobject][ordered]#{
Server = $singlerow[0]
Client = $singlerow[1]
"JobDirectory/Script" = $singlerow[2]
Policy = $singlerow[3]
}
}
Sample Output from $asObject | Format-List
Server : server1
Client : test.domain.com
JobDirectory/Script : "vmware:/?filter= VMHostName AnyOf "server2.domain.com", "server3.domain.com""
Policy : TEST
Another way using your starting code
$obj = gc c:\temp\test.csv |
% { $_ -replace '"(\b[^"]*\b)"','$1' } |
convertfrom-csv | % { [pscustomobject][ordered] #{
Server = $_.{Node Name}
Client = $_.{Client Name}
{JobDirectory/Script} = $_.{Job Directory}
Policy = $_.{Policy Name} }
}

WebClient.Downloadfile API is not working in powershell

(new-object System.Net.WebClient).Downloadfile("https://www.dropbox.com/sh/tsyz48qg0rq3smz/QAstBLgPgN/version.txt", "C:\Users\Brangle\Desktop\version.txt") API download invalid data.
version.txt file need to download. But actually it is downloading some xml file contains in version.txt on destination location
Thanks in advance
You are trying to download the dropbox page which presents your file in a nice dropbox-themed html. You need to extract the real url and can do so using the following code:
$wc = New-Object system.net.webclient;
$s = $wc.downloadString("https://www.dropbox.com/sh/tsyz48qg0rq3smz/QAstBLgPgN/version.txt");
$r = [regex]::matches($s, "https://.*token_hash.*(?=`")");
$realURL = $r[$r.count-1].Value;
$wc.Downloadfile($realURL, "U:\version.txt");
The regex part looks for a url starting https://, has a string token_hash in the middle and ends one character before double quotes character ". The line in question is:
FilePreview.init_text("https://dl.dropboxusercontent.com/sh/tsyz48qg0rq3smz/QAstBLgPgN/version.txt?token_hash=AAEGxMpsE-T4xodBPd3A6uPTCr0uqh7h4B2YUSmTDJHmjg", 0, null, 0)
Hope this helps.
Here is the function:
function download-dropbox($Url, $FilePath) {
$wc = New-Object system.net.webclient
$req = [System.Net.HttpWebRequest]::Create($Url)
$req.CookieContainer = New-Object System.Net.CookieContainer
$res = $req.GetResponse()
$cookies = $res.Cookies | % { $_.ToString()}
$cookies = $cookies -join '; '
$wc.Headers.Add([System.Net.HttpRequestHeader]::Cookie, $cookies)
$newurl = $url + '?dl=1'
mkdir (Split-Path $FilePath) -force -ea 0 | out-null
$wc.downloadFile($newurl, $tempFile)
}
Re: LogMeIn - theyuse a cookie base authentication so you can't use the previous code. Try this, it gets a cookie from the first response and then uses that to download using webclient:
$url = "https://secure.logmein.com/fileshare.asp?ticket=01_L5vwmOrmsS3mnxPO01f5FRbWUwVKlfheJ5HsfpTV"
$wc = New-Object system.net.webclient
$req = [System.Net.HttpWebRequest]::Create($url)
$req.CookieContainer = New-Object System.Net.CookieContainer
$res = $req.GetResponse()
$cookie = $res.Cookies.Name + "=" + $res.Cookies.Value
$wc.Headers.Add([System.Net.HttpRequestHeader]::Cookie, $cookie)
$newurl = $url + "`&download=1"
$wc.downloadFile($newurl, "c:\temp\temp.zip")

Combining Email code with if statement

I'm new to Powershell and am having trouble joining together two scripts I have.
What I want to do is check the length of all the csv files within a particular folder and if any of them are 0 Kb, I want to send off an alert email. So far I have a script which sends an email successfully and I have a script which checks the size successfully, but I am having trouble joining the two together.
Ideally it would send the name of the files which are empty in the body of the email.
The code below checks the file size and if it is greater than 1Kb it returns true.
$file = 'FilePath\File1.csv'
$Result = if (Test-Path $file) { (Get-Item $file).length -gt 1kb }
if ($Result -eq "True") {"File1.csv Contains Data"} ELSE {"File1.csv is Empty!"}
$file = 'FilePath\File2.csv'
$Result = if (Test-Path $file) { (Get-Item $file).length -gt 1kb }
if ($Result -eq "True") {"File2.csv Contains Data"} ELSE {"File2.csv is Empty!"}
$file = 'FilePath\File3.csv'
$Result = if (Test-Path $file) { (Get-Item $file).length -gt 1kb }
if ($Result -eq "True") {"File3.csv Contains Data"} ELSE {"File3.csv is Empty!"}
$file = 'FilePath\File4.csv'
$Result = if (Test-Path $file) { (Get-Item $file).length -gt 1kb }
if ($Result -eq "True") {"File4.csv Contains Data"} ELSE {"File4.csv is Empty!"}
$file = 'FilePath\FileName5.csv'
$Result = if (Test-Path $file) { (Get-Item $file).length -gt 1kb }
if ($Result -eq "True") {"File5.csv Contains Data"} ELSE {"File5.csv is Empty!"}
$file = 'FilePath\FileName6.csv'
$Result = if (Test-Path $file) { (Get-Item $file).length -gt 1kb }
if ($Result -eq "True") {"File6.csv Contains Data"} ELSE {"File6.csv is Empty!"}
Below is the email portion
$subject = "Emailtest"
$body = "test"
$emailTo = "jbloggs#Madeup.com"
$emailFrom ="JohnSmith#123.com"
$smtpServer = “mail.madeup.com”
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$credentials=new-object system.net.networkcredential(”username”,”password”)
$smtp.credentials=$credentials.getcredential($smtpserver,"25","basic")
$smtp.Send($emailFrom, $emailTo, $subject, $body)
Thank you for any help.
That's an awful lot of (manual) work just to check for empty files. What happens when you add a seventh - do you have to edit the script?
$EmptyFiles = (Get-childItem -Path $FilePath -Filter *.csv | `
where-object {$_.length -eq 0}|select-object -expandproperty Name)
$MsgBody = "The following files are empty:";
$EmptyFiles | foreach{$MsgBody+="`n$_";};
$MsgBody; # Just to output to console
$secpasswd = ConvertTo-SecureString "password" -AsPlainText -Force
$credentials= New-Object System.Management.Automation.PSCredential ("username", $secpasswd)
$subject = "Emailtest"
$body = "test"
$emailTo = "jbloggs#Madeup.com"
$emailFrom ="JohnSmith#123.com"
$smtpServer = “mail.madeup.com”
send-mailmessage -smtpserver $smtpServer -subject $subject -to $emailto -Credential $credentials -body $MsgBody

Parsing PDF by line

I've been able to parse a PDF by page multiple ways, the latest being this (not my code):
$reader = New-Object iTextSharp.text.pdf.pdfreader -ArgumentList "oldy.pdf"
for ($page = 1; $page -le $reader.NumberOfPages; $page++)
{
$strategy = new-object 'iTextSharp.text.pdf.parser.SimpleTextExtractionStrategy'
$currentText = [iTextSharp.text.pdf.parser.PdfTextExtractor]::GetTextFromPage($reader, $page, $strategy);
[string[]]$Text += [system.text.Encoding]::UTF8.GetString([System.Text.ASCIIEncoding]::Convert( [system.text.encoding]::default, [system.text.encoding]::UTF8, [system.text.Encoding]::Default.GetBytes($currentText)));
}
I found a post here that suggested using LocationTextExtractionStrategy instead and splitting each line out by '\n'
However, I will admit that the .NET code here is confusing me and i'm not sure how to modify it to parse by string.
Can anyone help?
thanks.
Only a first experiment, but it works as expected:
# Download http://sourceforge.net/projects/itextsharp/
Add-Type -Path itextsharp.dll
$reader = New-Object iTextSharp.text.pdf.pdfreader -ArgumentList MyFile.pdf
for ($page = 1; $page -le $reader.NumberOfPages; $page++)
{
# extract a page and split it into lines
$text = [iTextSharp.text.pdf.parser.PdfTextExtractor]::GetTextFromPage($reader,$page).Split([char]0x000A)
Write-Host "Page $($page) contains $($text.Length) lines. This is line 5:"
Write-Host $text[4]
#foreach ($line in $text)
#{
# any tasks
#}
}
$reader.Close()

Powershell in-line "if" for send-mailmessage

Is it possible in PowerShell to add a parameter on a cmdlet call ONLY if there is a variable to pass?
E.g.
Send-MailMessage -To $recipients (if($copy -ne "") -cc $copy) ....
Not the way you've written above but you can splat the parameters, building the hash with conditions, so you only have one call to send-mailmessage. An example from a script I wrote a few months ago:
#Set up default/standard/common parameters
$MailParams = #{
"Subject"="This is my subject";
"BodyAsHtml" = $true;
"From" = $MailFrom;
"To" = $MailTo;
"SmtpServer" = $SMTPServer;
};
#On the last day of the month, attach a logfile.
if ((Get-Date).AddDays(1).Day -eq 1) {
$attachment = $LogFilePath;
$ReportContent = "Full log for the the preceding month is attached.<br><br>" + $ReportContent;
$MailParams.Add("Attachments",$attachment);
}
send-mailmessage #MailParms
So in your case, it would be:
$MailParams = #{
"Subject"="This is my subject";
"From" = $MailFrom;
"To" = $recipients;
"SmtpServer" = $SMTPServer;
};
if (($copy -ne [string]::empty) -and ($copy -ne $null)) {
$MailParms.Add("CC",$copy);
}
send-mailmessage #MailParms

Resources