PS.Utilies is a c# .NET standard 2.0 set of powershell cmdlets that wraps the following utilities :-
- Git - wraps many usefull git commands
- Devops - Uses the azure api to manipulate azure devops projects/repositories and pipelines
- Excel - provides wrappers to read and write Excel files using the excellent Spread sheet Light library. Usefull for allowing users to populate input data using a toolthey are used tool.
- DotNet - access to dotnet cli
- Visual Studio - call visual studio command line (automatically finds the latest version of VS that is installed)
- Sql - added functionality for SQLServer PS Library
- Nuget - Update nuget default profile with credentials from devops
see: Spread sheet light
Install-Module PS.Utilities
| Cmdlet | Description | System |
|---|---|---|
| Set-DevopsCredentials | Sets session-wide credentials | Git/Devops |
| Find-DevopsRepository | locates the remote devops repository | Devops |
| Get-DevopsProjects | Returns a collection of devops projects | Devops |
| Get-DevopsProject | Returns a single project | Devops |
| Get-DevopsRepositories | Returns a collection of repositories from the given project | Devops |
| Get-DevopsRepository | Returns a Repository model | Devops |
| Get-DevopsPipelines | Returns a collection of pipelines | Devops |
| Invoke-DevopsPipeline | invokes the given pipeline | Devops |
| Wait-DevopsPipeline | Waits for a devops pipeline to complete | Devops |
| New-DevopsPullRequest | TO DO | |
| Complete-DevopsPullRequest | TO DO | |
| Test-Git | Test to see if git is installed | Git |
| Install-Git | Installs Git | Git |
| Copy-Repository | Clones a remote repository | Git |
| Find-Branch | Checks local branch | Git |
| Get-Branches | Gets a collection of local branches | Git |
| Switch-Branch | Checks out local branch | Git |
| Remove-Branch | Removes local branch | Git |
| New-Branch | Creates new local branch | Git |
| Save-Repository | Saves (commmits) local changes | Git |
| Push-Repository | pushes commited changes from local to remote repository | Git |
| Sync-Repository | Pulls latest changes to local repo | Git |
| Get-Tags | Gets a List of tags in the local repository | Git |
| New-Tag | Creates a new tag at the current commit of a local repository | Git |
| New-Excel | Creates a new Excel Application Instance | Excel |
| Open-Excel | Opens an existing spread sheet | Excel |
| Set-ExcelData | Populates the given worksheet with an array of PS class data | Excel |
| Set-ExcelRowProperty | Sets the properties of the specified row and wporksheet | Excel |
| Save-Excel | Saves the given excelapplication to an excel file | Excel |
| Get-ExcelData | Gets data from the given excelapplication and returns a .net List of type | Excel |
| Select-ExcelWorkSheet | Makes the given worksheet the default worksheet for other operations | Excel |
| Invoke-DotNetProcess | Runs the given local csproj project | DotNet |
| Start-DotNetCommand | Runs any dotnet cli command and returns the results | DotNet |
| Invoke-VisualStudioBuild | Builds a project or solution using visual studio (devenv) | Visual Studio |
| Start-VisualStudioCommand | runs the specified visual studio command line against the given project or solution | Visual Studio |
| Resolve-SqlResult | returns a list<> or single object mapped to a typed class from the result of Invoke-Sqlcmd call | Sql |
| Update-NugetConfig | updates default nuget entries with credentials |
Sets session-wide credentials for both git and devops. You should call this as the first cmdlet in your script. The credentials will be stored for the entire PS session. and is scoped for every function / cmdlet within that session.
Example
$patToken = "some-plain-text-Path-Token"
$userName = "aperson@org.com"
Set-DevopsCredentials -Username $userName -PlaintextPassword $patToken -Organisation "your-devops-organisation" -UseEnvironment
## to use existing credentials of a previously pulled repository you can use
Set-DevopsCredentials -Organisation -FromRepository "C:\\somelocalRepository"
# .. do your git/devops stuff here
Parameters
| Parameter | Description |
|---|---|
| -Username | (optional) Username to use to authenticate with git / azure |
| -PlainTextPassword | (optional if using -FromRepository) the PAT token to use to authenticate with remptes devops |
| -Organisation | the devops organisation name (i.e https://devop://dev.azure.com/organisation/blah) |
| -FromRepository | An existing repository to use (fropm the remote.url git config definition) |
| -UseEnvironment | (Switch parameter) if specified the specified credentials will be stored in (user) environment variables. All other Fundtions that need these credentials will use the environment variable values in the first instance |
Returns Nothing
locates the remote devops repository specified by the -Name parameter. It will search all projects within the organisation defined in Set-DevopsCredentials
Example
$repo = Find-DevopsRepository -Name $repoName
# use $repo.RemoteUrl to target remote url
public class RepositoryModel
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Url { get; set; }
public string DefaultBranch { get; set; }
public long Size { get; set; }
public string RemoteUrl { get; set; }
public bool IsDisabled { get; set; }
public Guid ProjectID { get; set; }
}
Parameters
| Parameter | Description |
|---|---|
| -Name | The nameof the remote repository |
Returns [RepositoryModel] The repository model found. Throws if not found
Returns a collection of devops projects from Set-DevopsCredentials Example
$projects = Get-DevopsProjects
public class ProjectModelCollection : List<ProjectModel>
{
public ProjectModel GetProjectByName(string name)
{
return this.FirstOrDefault(x => x.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
}
}
public class ProjectModel
{
public string Name { get; set; }
public Guid Id { get; set; }
public string Url { get; set; }
}
Returns [ProjectModelCollection] A collection of devops projects
Returns a project definition from Set-DevopsCredentials
Example
$project = Get-DevopsProject
public class ProjectModel
{
public string Name { get; set; }
public Guid Id { get; set; }
public string Url { get; set; }
}
Parameters
| Parameter | Description |
|---|---|
| -Projects | [ProjectModelCollection] (from Get-DevopsProjects) |
| -Name | The nameof the remote project to find |
Returns [ProjectModel] A devop projects
Returns a collection of repositories from a given devops project
Example
$projectRepositories = Get-DevopsRepositories -Name $projectName
foreach ($repo in projectRepositories) {
Write-Host "$($repo.RemoteUrl)"
}
public class RepositoryModel
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Url { get; set; }
public string DefaultBranch { get; set; }
public long Size { get; set; }
public string RemoteUrl { get; set; }
public bool IsDisabled { get; set; }
public Guid ProjectID { get; set; }
}
Parameters
| Parameter | Description |
|---|---|
| -Project | (optional) a devops project from a previous call to Get-DevopsProject(s) |
| -Name | the name of the project to retrieve the repositories from |
Returns [RepositoryModelCollection] A devop Repository
Returns a repository from the given project
Example
$projectRepository = Get-DevopsRepository -ProjectName $projectName -Name $repositoryName
public class RepositoryModel
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Url { get; set; }
public string DefaultBranch { get; set; }
public long Size { get; set; }
public string RemoteUrl { get; set; }
public bool IsDisabled { get; set; }
public Guid ProjectID { get; set; }
}
Parameters
| Parameter | Description |
|---|---|
| -Repositories | (optional) [RepositoryModelCollection] a collection of repositories from devops project from a previous call to Get-DevopsRepositories |
| -ProjectName | (optional) The name of the project that contains the project |
| -Name | the name of the repository to retrieve |
Returns [RepositoryModel] A devops Repository
Returns a collection of devopspipelines associated with the given project or all pipelines in the organisation
Example
# to get ALL pipelines in the current organisation
$allPipelines = Get-DevopsPipelines
# to get a specific pipeline (by name) across all projects
$pipeline = Get-DevopsPipelines -Name $pipelineModelName
#to get all pipelines in a specific project
$projectPipelines = Get-DevopsPipelines -Project $projectName
# or any combination of parameters !
public class PipelineModelCollection : List<PipelineModel>
{
}
public class PipelineModel
{
public string Url { get; set; }
public long Id { get; set; }
public string Name { get; set; }
public string Folder { get; set; }
public Guid ProjectId { get; set; }
}
Parameters
| Parameter | Description |
|---|---|
| -Projects | (optional) [ProjectModelCollection] a collection of projects |
| -ProjectName | (optional) The name of the project that contains the pipelines |
| -Name | the name of the pipeline to retrieve |
Returns [PipelineModelCollection] A collection of devops project pipelines
Invokes a devops pipeline and returns a pipeline response including the id of the pipeline
Example
# fron a pipeline model
$pipelineResult = Invoke-Pipeline -Pipeline $pipelineModel
# a specific pipeline name
$pipelineResult = Invoke-Pipeline -NAme $pipelineName
public class RunPipelineResponseModel
{
public string CreatedDateString { get; set; }
public DateTime CreatedDate { get; set; } = DateTime.MinValue;
public string FinishedDateString { get; set; }
public DateTime FinishedDate { get; set; } = DateTime.MinValue;
public long Id { get; set; }
public string Name { get; set; }
public string RunResultString { get; set; }
public string RunStateString { get; set; }
public RunResult RunResult { get; set; } = RunResult.Unknown;
public RunState RunState { get; set; } = RunState.Unknown;
public bool IsRunning { get; set; } = false;
public bool IsCompleted { get; set; } = false;
public bool HasWorked { get; set; } = false;
public Guid ProjectId { get; set; }
}
Parameters
| Parameter | Description |
|---|---|
| -Pipeline | (optional) [PipelineModel] pipeline model |
| -Name | the name of the pipeline to invoke |
Returns [RunPipelineResponseModel] The pipeline response
Waits for a devops pipeline to complete and returns an indicator [bool] as to wether it has completed successfully.
Example
$devopsPipelineResult = Invoke-DevopsPipeline -Name $myPipeline | Wait-DevopsPipeline
Write-Host "Pipeline has pipeline worked $($devopsPipelineResult)"
public class RunPipelineResponseModel
{
public string CreatedDateString { get; set; }
public DateTime CreatedDate { get; set; } = DateTime.MinValue;
public string FinishedDateString { get; set; }
public DateTime FinishedDate { get; set; } = DateTime.MinValue;
public long Id { get; set; }
public string Name { get; set; }
public string RunResultString { get; set; }
public string RunStateString { get; set; }
public RunResult RunResult { get; set; } = RunResult.Unknown;
public RunState RunState { get; set; } = RunState.Unknown;
public bool IsRunning { get; set; } = false;
public bool IsCompleted { get; set; } = false;
public bool HasWorked { get; set; } = false;
public Guid ProjectId { get; set; }
}
Parameters
| Parameter | Description |
|---|---|
| -Pipeline | (optional) [PipelineModel] pipeline model |
| -Name | the name of the pipeline to invoke |
Returns [RunPipelineResponseModel] The pipeline response
Tests to see if git is installed.
Example
if (Test-Git) {
# do something with git
} else {
Install-Git -AutoInstall
}
Returns the full path of the git executable OR null if it does not exist
Downloads and optionally installs the latest verion of git for windows
Example
$gitExe = Install-Git -AutoInstall
Parameters
| Parameter | Description |
|---|---|
| -AutoInstall | switch parameter which (if specified) installs git to the local users directory |
Returns [string] the full path of the git executable
Git Clones a remote devops repository to the specified local directory
Example
$repoName = "my-repo-name"
$repo = Find-DevopsRepository -Name $repoName
$repositoryPath = Copy-Repository -Url $repo.RemoteUrl `
-Directory "C:\Deleteme" `
-Force
Parameters
| Parameter | Description |
|---|---|
| -Url | the remote url of the repository to clone (see Find-DevopsRepository to get this from a repo name |
| -User | the user name to authenticate (not required if Set-DevopsCredentials is used |
| -Password | the password to authenticate (not required if Set-DevopsCredentials is used |
| -Directory | the local directory to clone into |
| -Branch | the branch to pull |
| -Force | (switch parameter) if the repos already exists it will be deleted and re-created |
Returns [string] the full path of the local repository
indicates if the specified local branch of the given directory exists.
Example
if (Find-Branch -Directory $repositoryPath -Branch $branch) {
# do something because local branch exists
}
Parameters
| Parameter | Description |
|---|---|
| -Directory | the local repo directory |
| -Branch | the branch to find |
Returns [bool] true if branch exists
Gets a list of branches for the specified local reporitory
Example
$branches = Get-Branches -DirectoryName $repoName
foreach ($branch in $branches) {
Write-Host "$($branch.Path)"
}
public class GitBranch
{
public string Name { get; set; }
public bool IsRemote { get; set; } = false;
public bool IsCurrent { get; set; } = false;
public string Path { get; set; }
}
Parameters
| Parameter | Description |
|---|---|
| -Directory | Local path of local repository |
Returns [GitBranchCollection] Array {List<>} of GitBranch
Checks out the given branch from the given local repository
Example
if (Find-Branch -Directory $repositoryPath -Branch $branch) {
$rc = Switch-Branch -Directory $repositoryPath -Branch $branch
}
Parameters
| Parameter | Description |
|---|---|
| -Directory | the local repo directory |
| -Branch | the branch to find |
Returns [int] the return code of the switch operation
Removes the specified local branch
Example
Remove-Branch -Directory $repositoryPath -Branch $branch -Force
Parameters
| Parameter | Description |
|---|---|
| -Directory | the local repo directory |
| -Branch | the branch to find |
| -Force | (Switch) remove the branch even if there are outstanding changes/commits |
Returns [int] return code of the delete operation
Creates a new branch for the given local repository AND checks it out. If a remote branch with the same name exists, it will checkout that branch
Example
# get remote repo
$repo = Find-DevopsRepository -Name $repoName
# create new branch based on the default remote repo branch (main/master etc)
New-Branch -Directory $repositoryPath -Branch $branch -BaseBranch $repo.DefaultBranch
Parameters
| Parameter | Description |
|---|---|
| -Directory | the local repo directory |
| -Branch | the branch to find |
| -BaseBranch | The base branch from which to create the new branch from |
Returns [int] return code of the new branch operation
Commits any changes to the given local repository
Example
Save-Repository -Directory $repositoryPath -Message "A Commit message"
Parameters
| Parameter | Description |
|---|---|
| -Directory | the local repo directory |
| -Message | The commit message |
Returns [int] return code of the save operation
Pushes changes to the remote repository.
Example
Push-Repository -Directory $repositoryPath -Branch $branch
Parameters
| Parameter | Description |
|---|---|
| -Directory | the local repo directory |
| -Branch | (optional) use if the branch you want to push to is NOT the current branch |
Returns [int] return code of the save operation
Pulls latest changes to local repository for the given branch.
Example
$rc = Sync-Repository -Directory $repositoryPath -Branch $branch
Parameters
| Parameter | Description |
|---|---|
| -Directory | The local repo directory |
| -Branch | (optional) Branch you want to pull changes for |
Returns [int] return code of the pull operation
Gets a collection of tags for the given local repository.
Example
$tags = Get-Tags -Directory $repositoryPath -Branch $branch
Parameters
| Parameter | Description |
|---|---|
| -Directory | The local repo directory |
| -Branch | (optional) Branch you want to get tags for |
Returns [List<system.string>] List of tags associated with this repo/branch
Creates a new Tag at the current commit of a local repository
Example
$rc = New-Tag -Directory $repositoryPath -Tag "23234" -Message "Tag description"
Parameters
| Parameter | Description |
|---|---|
| -Directory | The local repo directory |
| -Tag | The tag name to add to the current commit |
| -Message | (optional) description to add to the tag |
Returns [int] Return code of the tag operation
Creates a new Excel Application and returns [ExcelApplication]
Example
# create an empty excelapplication
$excelApp = New-Excel
# create a new excel application and create new (default) worksheet named "worksheet1"
# set the hilight colour and freeze the top rwo
$excelApp = New Excel -Worksheet "worksheet1" -HilightColour LightGray -FreezeTopRow
Parameters
| Parameter | Description |
|---|---|
| -Worksheet | (optional) create a new worksheet |
| -HilightColour | (optional switch) set the system.drawing.color (named) colour for highlighted rows\columns |
| -FreezeTopRow | (optional switch) set the top row to be frozen when the user scrools the worksheet |
Returns [ExcelApplication] instance of an excel application
Opens an existing excel file and returns [ExcelApplication]
Example
$excelApp = New-Excel | Open-Excel -Filename "C:\\SomeFile.xlsx"
Parameters
| Parameter | Description |
|---|---|
| -Application | [ExcelApplication] |
Returns [ExcelApplication] instance of the excel application
Populates an excel Application worksheet (or default) with the contents of a PS class
Example
class TestExcelData {
[string]$Excel_String_Column
[decimal]$Excel_Decimal_Column
TestExcelData() {}
TestExcelData([string]$Col1, [decimal]$Col2) {
$this.Excel_String_Column = $Col1
$this.Excel_Decimal_Column = $Col2
}
$dataAray = @()
$dataAray += New-Object TestExcelData -ArgumentList "row 1 column 1", 1.0
$dataAray += New-Object TestExcelData -ArgumentList "row 1 column 2", 1.1
$excelApp = New-Excel | Set-ExcelData -Data $dataAray -UsePropertyNameAsHeader -AutoFitColumns
}
Parameters
| Parameter | Description |
|---|---|
| -Application | [ExcelApplication] |
| -Data | PS class Array of data |
| -UsePropertyNameAsHeader | (optional) the property names of the input class will be used as the header names (row 1) of the worksheet |
| -AutoFitColumns | (Optional) (SwitchParameter) alter columns to fit their content |
| -Worksheet | (optional) Switches to or creates a worksheet to hold the class data |
Returns [ExcelApplication] instance of the excel application
Sets the row properties of a given row and or column returns [ExcelApplication] Must be called AFTER a spreadsheet has been populated
Example
# bold every column in row 0 (header column)
$excelApp = $app | Set-ExcelRowProperty -Row 0 -Format Bold
# highlight row 1 column 3
$excelApp = $app | Set-ExcelRowProperty -Row 0 -Column 3 -Format HiLight
Parameters
| Parameter | Description |
|---|---|
| -Application | [ExcelApplication] |
| -Row | row number (starts at 1) |
| -Column | optional column to apply yhr format to |
| -Format | formats the row/column to one of Hilight/Bold/None |
| -Worksheet | (Optional) The names of the worksheet |
Returns [ExcelApplication] instance of the excel application
Saves the ExcelApplication to the given filename and returns [ExcelApplication]
Example
New-Excel | Save-Excel -Filename "C:\\somewhere.xlsx"
Parameters
| Parameter | Description |
|---|---|
| -Application | [ExcelApplication] |
| -Filename | excel filenane.(replaced if it already exists) |
Returns a PS.Class array of data from an existing worksheetand returns [ExcelApplication] note each property of the given type is matched with a row header value. so only the columns that macth a property are returned
Example
class TestExcelData {
[string]$Excel_String_Column
[decimal]$Excel_Decimal_Column
TestExcelData() {}
TestExcelData([string]$Col1, [decimal]$Col2) {
$this.Excel_String_Column = $Col1
$this.Excel_Decimal_Column = $Col2
}
$data = New-Excel `
| Open-Excel -Filename $excelFile `
| Get-ExcelData -DataType TestExcelData -Worksheet "David"
foreach ($dat in $data) {
Write-Host "$($dat.Excel_String_Column) $($dat.Excel_Decimal_Column)"
}
Parameters
| Parameter | Description |
|---|---|
| -Application | [ExcelApplication] |
| -DataType | Type of the array to return. |
| -Worksheet | (Optional) name of worksheet to extract data from |
Returns [Array] .net generic list of -DataType
Selects the given worksheet in the ExcelApplication instance and returns [ExcelApplication]
Example
$excelApp = New-Excel `
| Open-Excel -Filename $excelFile `
| Select-ExcelWorkSheet -Worksheet "David"
Parameters
| Parameter | Description |
|---|---|
| -Application | [ExcelApplication] |
| -Worksheet | Name of worksheet to use |
Returns [ExcelApplication] The excelapplication instance
Runs the given dotnet process on the current machine (e.g for micro services)
Example
$rootProj = "C:\\somedirectory\\subdir\\"
$projToRun = "$($rootProj)myconsoleapp.csproj"
New-DotNetProcess -Project $projToRun `
-WindowStyle Minimized `
-Pull ` # pull lates version first
-Name "My Program" # -Wait
Parameters
| Parameter | Description |
|---|---|
| -Project | Full path to project |
| -WindowStyle | Window style of console app (Minimized,Normal etc) |
| -Pull | Pull the latest version of the 'current' branch |
| -Branch | Switch to this branch (before run/pull) |
| -Name | Update the window text name |
Returns [Process] The process that is started
Runs the given dotnet command within the same process OR in a new command window
Example
$dotnetCommand = "--version"
Start-DotNetCommand -Command $dotnetCommand `
-Directory "C:\\somehere\\nice" `
-NewWindow ` # run in new external cmd process
-WindowStyle Minimized
#-wait if speified waits for the command to complete if newWindow specified
Parameters
| Parameter | Description |
|---|---|
| -Command | full dotnet cli command |
| -Directory | optional starting directory |
| -NewWindow | (switch parameter) if specified starts the dotnet command in a seperate process |
| -WindowStyle | for external process what type of winow (Normal, Mazimized etc) |
| -Wait | (switch parameter) if specified waits for the process to end (if NewWindow is specified) |
| -Console | (switch parameter) if (specified and it is Not an external process the result of the command is returned as an array of string (list<>) |
Returns
[Process] if (-NewWindow) is specified it returns the Process associated with the command window
[List<>] if -Console specified the result of the command is returned as a generic strin glist
[rc] if the command fails the return code is returned
Builds the given project using the latest version of Visual Studio that is installed on the local machine. This tends to work better than dotnet build and msbuild
Example
$vsProject = "C:\\somewhere\nic\my.csproj"
$consoleLogs = Invoke-VisualStudioBuild -Project $vsProject `
-Console
Parameters
| Parameter | Description |
|---|---|
| -Project | full path to the solution or project file to build |
| -Console | (switch parameter) if specified returns a list<> of the ouput from the build |
Returns
[List<>] if (-Console) is specified it returns the List<> (string) of the output
[int] if (-Console) is not spefified returns the return code of the process
Executes the given devenv command against the given solution or project using the latest version of Visual Studio that is installed on the local machine. This tends to work better than dotnet build and msbuild
Example
$vsProject = "C:\\somewhere\nic\my.csproj"
$args = "/clean"
$consoleLogs = Start-VisualStudioCommand -Project $vsProject `
-Arguments $args `
-Console
Parameters
| Parameter | Description |
|---|---|
| -Project | full path to the solution or project file to build |
| -Arguments | list of argument to pass to visual studio |
| -Console | (switch parameter) if specified returns a list<> of the ouput from the build |
Returns
[List<>] if (-Console) is specified it returns the List<> (string) of the output
[int] if (-Console) is not spefified returns the return code of the process
Takes the result of a Invoke-Sqlcmd call and returns a populated (typed) object which can be a single object or an array of objects. Only the properties specified in the -ResultType are returned (case-insensitive)
Example
class SqlResult {
[Guid]$Id
[string]$Name
}
$sqlconnection = "server=xxx; User Id=blah ..."
$sql = "select * from xxx where yyy = 'asasd"
$result = Invoke-Sqlcmd -ConnectionString $sqlConnection -Query $sql
$resultRows = Resolve-SqlResult -Result $result -ResultType SqlResult
foreach ($row in resultRows) {
Write-Host "$($row.Id) $($row.Name)"
}
Parameters
| Parameter | Description |
|---|---|
| -Result | The result from the Invoke-Sqlcmd call |
| -ResultType | The class type to populate with the results (single or list<>) |
Returns
[List<>] of ResultType
[object] of ResultType
Updates the default nuget config (%appdata%\nuget\nugetconfig) with credentials from the Set-DevopsCredentials OR from parameters passed to it
Example
# update from previous call to set-devopscredentials
Update-NugetConfig
# update any nugets uri's that contain the given Organisation with username/password
Update-NugetConfig -Organisation xxxxxxx -Username yyyyy -PlaintextPassword zzzzzzz
# update specifc uri
Update-NugetConfig -Url xxxxxxx -Username yyyyy -PlaintextPassword zzzzzzz
Parameters
| Parameter | Description |
|---|---|
| -Organisation | (optional) Azure devops organisation id |
| -Username | (optional) user name |
| -PlaintextPassword | (optional) plain text PAT token |
| -Name | Nuget config entry by name |
| -Url | Nuget config entry by Url |
Returns
[int] The return code of the operation
Import-Module PS.Utilities
$patToken = "YOUR-PAT-TOKEN"
$org = "YOUR-DEVOPS-ORGANISATION"
# set credentials for git and azure devops based on a pat token
# lasts for the duration of the powershell session
Set-DevopsCredentials -PlainTextPassword $patToken -Organisation $org
$repoName = "Test-Repo"
# locate the repository within all of the projects available to the users pat token auth
$repo = Find-DevopsRepository -Name $repoName
# clone the repository locally (delete if already there)
$repositoryPath = Copy-Repository -Url $repo.RemoteUrl `
-Directory "C:\Deleteme" `
-Force
Write-Host "Repository cloned to $($repositoryPath) "
$branch = "DavidTest"
Write-Host "Creating Branch $($branch)"
# if there is not a branch defined - delete it
if (Find-Branch -Directory $repositoryPath -Branch $branch) {
Remove-Branch -Directory $repositoryPath -Branch $branch -Force
}
# re-create the branch - NOTE if there is a remote branch with the same name it will checkout that branch instead
New-Branch -Directory $repositoryPath -Branch $branch -BaseBranch $repo.DefaultBranch
# add a new text file to the local repo within the newly created branch
Write-Host "Add test file to new Branch"
Get-Date | Out-File -FilePath (Join-Path -Path $repositoryPath -ChildPath "testcommit.txt") -Append
Write-Host "Commit A test file"
# commit the changes locally
Save-Repository -Directory $repositoryPath -Message "A test for david"
Write-Host "Push Changes"
# push the directory to remote devops instance
Push-Repository -Directory $repositoryPath -Branch $branch
# create a pull request AND complete it! - not a real world example
$pullRequestResponse = New-DevopsPullRequest -Directory $repositoryPath -Repository $repoName -SourceBranch $branch -TargetBranch "main" -Title "Test request title" -Description "Test pull request from powershell" | Complete-DevopsPullRequest -Verbose
Write-Host ($pullRequestResponse | Format-List | Out-String)
Write-Host ($pullRequestResponse.CreatedBy | Format-List | Out-String)
Import-Module PS.Utilities
class TestExcelData {
[string]$Excel_String_Column
[decimal]$Excel_Decimal_Column
TestExcelData() {}
TestExcelData([string]$Col1, [decimal]$Col2) {
$this.Excel_String_Column = $Col1
$this.Excel_Decimal_Column = $Col2
}
}
$global:excelFile = "C:\\deleteme\\TestExcelPS.xlsx"
# create a excel spreadsheet
function CreateSpreadsheet() {
$dataAray = @()
$dataAray += New-Object TestExcelData -ArgumentList "hello 1 this is a very long to see if it will expand ",123.1
$dataAray += New-Object TestExcelData -ArgumentList "hello 2",3123123.1
$excelApp = New-Excel -Worksheet "David" -FreezeTopRow -HilightColour LightGray `
| Set-ExcelData -Data $dataAray -UsePropertyNameAsHeader -AutoFitColumns `
| Set-ExcelRowProperty -Row 0 -Format Hilight ` # highlight header row
| Save-excel -Filename $excelFile
}
function LoadSpreadsheet() {
# load the spreadhseet and display the contents
$data = New-Excel `
| Open-Excel -Filename $excelFile `
| Select-ExcelWorkSheet -Worksheet "David" `
| Get-ExcelData -DataType TestExcelData
foreach ($dat in $data) {
Write-Host "$($dat.Excel_String_Column) $($dat.Excel_Decimal_Column)"
}
}
CreateSpreadsheet
LoadSpreadsheet