Public/Start-AutotaskSession.ps1
<#
.Synopsis Cmdlet for automatically connecting to ATWS in scripts with a verified ATWS account. .DESCRIPTION Cmdlet for automatically connecting to ATWS in scripts. ATWS username needs to be entered in script located and password provided in account (username and password) for initialization and login. Username should be passed as a parameter. Password should be provided as a path to a clear text file with the password only as content. The file will be deleted after first run, and the password will be stored encrypted as secure string for later use. It will only be readable for the same user that created it, so be careful with the user context when running scripts automatically. .EXAMPLE Example of how to use this cmdlet .INPUTS Inputs to this cmdlet (if any) .OUTPUTS Output from this cmdlet (if any) .NOTES General notes .COMPONENT The component this cmdlet belongs to .ROLE The role this cmdlet belongs to .FUNCTIONALITY The functionality that best describes this cmdlet #> function Start-AutotaskSession { [CmdletBinding(PositionalBinding=$true)] Param ( # Autotask User Account [Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)] $ATWSuser, # Path to the initialization password file [Parameter(Mandatory=$false, ValueFromPipeline=$true, Position=1)] [ValidateScript({ if(-Not ($_ | Test-Path) ){ throw "File or folder does not exist" } if(-Not ($_ | Test-Path -PathType Leaf) ){ throw "The Path argument must be a file. Folder paths are not allowed." } return $true })] [System.IO.FileInfo]$InitPath, [Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=2)] [ValidateScript({ if(-Not ($_ | Test-Path) ){ throw "File or folder does not exist" } return $true })] [System.IO.FileInfo]$SecureStringPath ) # Sørger for nødvendige credentials ved initialisering $initpassword = "ATWSpassword.txt" if(Test-Path -Path "$SecureStringPath\ATWS.txt"){ $ATWSpwd = Get-Content "$SecureStringPath\ATWS.txt" $ATWSsecurePwd = $ATWSpwd | ConvertTo-SecureString $ATWSCred = New-Object System.Management.Automation.PSCredential -ArgumentList $ATWSuser, $ATWSsecurePwd } # end if else{ $ATWSpwd = Get-Content "$InitPath\$initpassword" $ATWSsecureString = ConvertTo-SecureString $ATWSpwd -AsPlainText -Force $ATWSsecurePwd = ConvertFrom-SecureString $ATWSsecureString Set-Content "$SecureStringPaths\ATWS.txt" $ATWSsecurePwd $ATWSCred = New-Object System.Management.Automation.PSCredential -ArgumentList $ATWSuser, $ATWSsecureString Remove-Item "$InitPath\$initpassword" } #end else # Kopler til Autotask API Connect-AutotaskWebAPI -Credential $ATWSCred } |