Showing posts with label powershell. Show all posts
Showing posts with label powershell. Show all posts

Wednesday

here is powershell script on how to get list of files from changesets associated with one tfs task

  
$dllPath = "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer"
Add-Type -Path "$dllPath\Microsoft.TeamFoundation.Client.dll"
Add-Type -Path "$dllPath\Microsoft.TeamFoundation.WorkItemTracking.Client.dll"
Add-Type -Path "$dllPath\Microsoft.TeamFoundation.VersionControl.Client.dll"

# Connect to TFS
$tfsUri = "https://tfs2.domain.net/tfs/project"
$username = "DOMAIN1\user1"     # or just "your-username" if no domain
$password = "-password-here"

# Create secure credentials
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$credentials = New-Object System.Net.NetworkCredential($username, $securePassword)

# Create TFS connection using credentials
$uri = New-Object System.Uri($tfsUri)
$tfs = New-Object Microsoft.TeamFoundation.Client.TfsTeamProjectCollection($uri, $credentials)

$tfs = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollection]::new($tfsUri)
$tfs.EnsureAuthenticated()

# Get services
$workItemStore = $tfs.GetService([Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore])
$versionControl = $tfs.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer])

# Get work item (replace with your Task ID)
$workItem = $workItemStore.GetWorkItem(29574)

$dict=@{}
# Loop over links to find associated changesets
foreach ($link in $workItem.Links) {
    if ($link -is [Microsoft.TeamFoundation.WorkItemTracking.Client.ExternalLink] -and
        $link.ArtifactLinkType.Name -eq "Fixed in Changeset") {

        $csId = [int]($link.LinkedArtifactUri -replace ".+changeset\/", "")
        $changeset = $versionControl.GetChangeset($csId)

        #Write-Output "Changeset $csId"
        foreach ($change in $changeset.Changes) {
            #Write-Output " - $($change.Item.ServerItem)"
            $key=$change.Item.ServerItem
            if (-not $dict.ContainsKey($key)) {
                $dict[$key] = $csId
}
            }
    }
}
foreach ($key in $dict.Keys) {
    #Write-Output "$key : $($dict[$key])"
    Write-Output $key
    Add-Content -Path "output.txt" -Value $key
}

add new fields to csv file using powershell


$z = Import-Csv zerotrac.csv
$nums= Import-Csv allleetcode.csv
$md=@{}
#converting one csv into hashmap for quicker search
foreach($r in $nums){
    $md.add($r.frontendQuestionId,$r)
}

foreach($r in $z){
    $nr=$md[$r.id]
    if ($nr){
     $r | Add-Member -MemberType NoteProperty -Name "isPaidOnly" -Value $nr.isPaidOnly
     $r | Add-Member -MemberType NoteProperty -Name "difficulty" -Value $nr.difficulty
     $r | Add-Member -MemberType NoteProperty -Name "topicTags" -Value $nr.topicTags

}

}


$z | Export-Csv -Path "C:\temp\newzerotrac1.csv" -NoTypeInformation

powershell compare two csv files and output differences

 I had to compare data returned from two APIs - one JSON another XML so

1. I used this  viewer to convert json to csv and this app to save xml to csv

2. I used this PowerShell to show difference between two csv files:


Start-Transcript -Path result-10.csv


$mas = Import-Csv 1.csv
$marko = Import-Csv 2.csv

$md=@{}
#converting one csv into hashmap for quicker search
foreach($r in $mas){
    foreach($a in $r.psobject.properties){
        $fn=$a.name
        $val=$a.value
        $md.Add($fn.ToLower(),$val)
    }
}

echo "fieldName,csv1,csv2"

    foreach($r in $marko){
        foreach($a in $r.psobject.properties){

            $k=$a.name.replace("_","").toLower()
            if ($md.ContainsKey($k)){
                if ($md[$k] -ne $a.value){
                  echo "$($a.name),$($a.value),$($md[$k])"
                }
            }
        }
    }
Stop-Transcript

Monday

powershell sitecore remove item from pipe separated list

here is function how to do it :
   
[string]$test ="{uka}|{taka}|{4aka}|{boom}"


function RemovePipeElement {
    param (
        [string]$body,
        [string]$elem

    )
    [string]$res=$body;
    [int]$a=$body.IndexOf($elem)
    if ($a -gt -1) {
        [string]$newbod="";
        [bool] $first=$TRUE;
        $arr=$body.split("|");

        foreach ($e in $arr) {
            if ($e -ne $elem ) {
                if ($first){
                    [string]$newbod=$e;
                    $first=$FALSE;
                }else{
                    [string]$newbod=$newbod+"|"+$e;
                }
            }
        }
        $res=$newbod;
    }

        return $res;
}

 Write-Host "$test"

 [string]$uid=RemovePipeElement $test "{uka}";
 Write-Host "$uid"

 [string]$uid=RemovePipeElement $test "{4aka}";
 Write-Host "$uid"

 [string]$uid=RemovePipeElement $test "{boom}";
 Write-Host "$uid"

 [string]$uid=RemovePipeElement $test "{taka}";
 Write-Host "$uid"

Tuesday

Running PowerShell script from C#



private void testRunToolStripMenuItem_Click(object sender, EventArgs e)
{
List<string> ls = new List<string>();
RunPowershellScript(@"c:\Program Files\Microsoft Transporter Tools\PassCh.ps1",ls);
//log("script complete");

}

private static void RunPowershellScript(string scriptFile, List<string> parameters)
{
// Validate parameters
if (string.IsNullOrEmpty(scriptFile)) { throw new ArgumentNullException("scriptFile"); }
if (parameters == null) { throw new ArgumentNullException("parameters"); }

RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
PSSnapInException ex;
runspaceConfiguration.AddPSSnapIn("Microsoft.Exchange.Transporter",out ex);

using (Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration))
{
runspace.Open();
RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
scriptInvoker.Invoke("Set-ExecutionPolicy Unrestricted");
Pipeline pipeline = runspace.CreatePipeline();
Command scriptCommand = new Command(scriptFile);
Collection<CommandParameter> commandParameters = new Collection<CommandParameter>();
foreach (string scriptParameter in parameters)
{
CommandParameter commandParm = new CommandParameter(null, scriptParameter);
commandParameters.Add(commandParm);
scriptCommand.Parameters.Add(commandParm);
}
pipeline.Commands.Add(scriptCommand);
Collection<PSObject> psObjects;
psObjects = pipeline.Invoke();
}
}

more...

Thursday

get list of installed software on computer

with Powershell

:


$Keys = Get-ChildItem HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall
$Items = $keys |foreach-object {Get-ItemProperty $_.PsPath}
foreach ($item in $items)
{
echo $item.Displayname
echo $item.DisplayVersion
echo $item.Publisher
echo $item.InstallDate
echo $item.HelpLink
echo $item.UninstallString
}

more...

make ubuntu business casual

to make ubuntu business casual - make it black remove background: gsettings set org.gnome.desktop.background picture-options ...