Merge pull request #115 from PetriAsi/feature/prefix-commands

Helper function to update existing SnipeItPS scripts
This commit is contained in:
Petri Asikainen 2021-05-24 11:09:50 +03:00 committed by GitHub
commit 4c230807aa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 233 additions and 40 deletions

View file

@ -0,0 +1,38 @@
Function Get-SnipeItAlias(){
@{'Get-Asset' = 'Get-SnipeItAsset';
'Get-AssetMaintenance' = 'Get-SnipeItAssetMaintenance';
'Get-Category' = 'Get-SnipeItCategory';
'Get-Company' = 'Get-SnipeItCompany';
'Get-Component' = 'Get-SnipeItCompany';
'Get-CustomField' = 'Get-SnipeItCustomField';
'Get-Department' = 'Get-SnipeItDepartment';
'Get-Fieldset' = 'Get-SnipeItFieldset';
'Get-Manufacturer' = 'Get-SnipeItManufacturer';
'Get-Model' = 'Get-SnipeItModel';
'Get-Status' = 'Get-SnipeItStatus';
'Get-Supplier' = 'Get-SnipeItSupplier';
'Get-User' = 'Get-SnipeItUser';
'New-Asset' = 'New-SnipeItAsset';
'New-AssetMaintenance' = 'New-SnipeItAssetMaintenance';
'New-Category' = 'New-SnipeItCategory';
'New-Component' = 'New-SnipeItComponent';
'New-CustomField' = 'New-SnipeItCustomField';
'New-Department' = 'New-SnipeItDepartment';
'New-License' = 'New-SnipeItLicense';
'Set-License' = 'Set-SnipeItLicense';
'New-Location' = 'New-SnipeItLocation';
'New-Manufacturer' = 'New-SnipeItManufacturer';
'New-Model' = 'New-SnipeItModel';
'New-User' = 'New-SnipeItUser';
'Set-Asset' = 'Set-SnipeItAsset';
'Set-AssetOwner' = 'Set-SnipeItAssetOwner';
'Set-Component' = 'Set-SnipeItComponent';
'Set-Model' = 'Set-SnipeItModel';
'Set-Info' = 'Set-SnipeItInfo';
'Set-User' = 'Set-SnipeItUser';
'New-Accessory' = 'New-SnipeItAccessory';
'Set-Accessory' = 'Set-SnipeItAccessory';
'Get-Accessory' = 'Get-SnipeItAccessory';
'Remove-Asset' = 'Remove-SnipeItAsset';
'Remove-User' = 'Remove-SnipeItUser';}
}

View file

@ -6,46 +6,11 @@ function Set-SnipeItAlias()
)]
param()
Write-Verbose "Setting compatibility aliases.. "
$aliases= @{'Get-Asset' = 'Get-SnipeItAsset';
'Get-AssetMaintenance' = 'Get-SnipeItAssetMaintenance';
'Get-Category' = 'Get-SnipeItCategory';
'Get-Company' = 'Get-SnipeItCompany';
'Get-Component' = 'Get-SnipeItCompany';
'Get-CustomField' = 'Get-SnipeItCustomField';
'Get-Department' = 'Get-SnipeItDepartment';
'Get-Fieldset' = 'Get-SnipeItFieldset';
'Get-Manufacturer' = 'Get-SnipeItManufacturer';
'Get-Model' = 'Get-SnipeItModel';
'Get-Status' = 'Get-SnipeItStatus';
'Get-Supplier' = 'Get-SnipeItSupplier';
'Get-User' = 'Get-SnipeItUser';
'New-Asset' = 'New-SnipeItAsset';
'New-AssetMaintenance' = 'New-SnipeItAssetMaintenance';
'New-Category' = 'New-SnipeItCategory';
'New-Component' = 'New-SnipeItComponent';
'New-CustomField' = 'New-SnipeItCustomField';
'New-Department' = 'New-SnipeItDepartment';
'New-License' = 'New-SnipeItLicense';
'Set-License' = 'Set-SnipeItLicense';
'New-Location' = 'New-SnipeItLocation';
'New-Manufacturer' = 'New-SnipeItManufacturer';
'New-Model' = 'New-SnipeItModel';
'New-User' = 'New-SnipeItUser';
'Set-Asset' = 'Set-SnipeItAsset';
'Set-AssetOwner' = 'Set-SnipeItAssetOwner';
'Set-Component' = 'Set-SnipeItComponent';
'Set-Model' = 'Set-SnipeItModel';
'Set-Info' = 'Set-SnipeItInfo';
'Set-User' = 'Set-SnipeItUser';
'New-Accessory' = 'New-SnipeItAccessory';
'Set-Accessory' = 'Set-SnipeItAccessory';
'Get-Accessory' = 'Get-SnipeItAccessory';
'Remove-Asset' = 'Remove-SnipeItAsset';
'Remove-User' = 'Remove-SnipeItUser';}
Write-Verbose "All aliases are deprediated."
ForEach ($key in $aliases.Keys ) {
New-Alias -Name $key -Value $($aliases[$key]) -Scope 1
Write-Verbose ("{0,5} -> {1}" -f $key,$($aliases[$key]))
$SnipeItAliases = get-SnipeItAlias
ForEach ($key in $SnipeItAliases.Keys ) {
New-Alias -Name $key -Value $($SnipeItAliases[$key]) -Scope 1
Write-Verbose ("{0,5} -> {1}" -f $key,$($SnipeItAliases[$key]))
}
Write-Verbose "Please start using native 'SnipeIt' prfixed functions instead."
Get-Command -Module SnipeItPS -CommandType Alias | Select-Object -Property DisplayName,ResolvedCommand

View file

@ -9,5 +9,7 @@ function Test-SnipeItAlias()
)
if($invocationName -ne $commandName) {
Write-Warning "$invocationName is still working, but it has been superceded by $commandName, please use it instead."
Write-Warning 'To update your scripts you can use Update-SnipeItAlias helper function: '
Write-Warning '"Get-Content [your-script.ps1] | Update-SnipeItAlias | Out-File [new-script-name.ps1]"'
}
}

View file

@ -0,0 +1,54 @@
<#
.SYNOPSIS
Replaces old SnipeItPS commands with new ones
.DESCRIPTION
Replaces old SnipeItPS commands with new ones
.PARAMETER String
Input string
.EXAMPLE
Get-Content [your-script.ps1] | Update-SnipeItAlias | Out-File [new-script-name.ps1]
Replaces old command from file "your-script.ps1" and creates new script "new-script-name.ps1"
After testing new file you can replace old file with new.
#>
function Update-SnipeItAlias()
{
[CmdletBinding(
SupportsShouldProcess = $true,
ConfirmImpact = "Low"
)]
param(
[Parameter(Mandatory = $true,
ValueFromPipeline = $true)]
[string[]]
$String
)
begin{
Write-Verbose "Replacing old SnipeIt fuctions with new ones.. "
$SnipeItAliases = Get-SnipeItAlias
}
process {
If ($PSCmdlet.ShouldProcess("ShouldProcess?")) {
ForEach ($st in $String){
$result = $st
ForEach ($key in $SnipeItAliases.Keys ) {
#Write-Verbose "Replacing $key with $($SnipeItAliases[$key])"
$result = $result -replace $key, $SnipeItAliases[$key]
}
$result
}
}
}
end{
Write-Verbose "..replacing done"
}
}

View file

@ -110,7 +110,9 @@ FunctionsToExport = @(
'Set-SnipeItAccessory',
'Get-SnipeItAccessory',
'Remove-SnipeItAsset',
'Remove-SnipeItUser'
'Remove-SnipeItUser',
'Update-SnipeItAlias'
)
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.

View file

@ -14,5 +14,43 @@ Get-ChildItem $scriptRoot *.ps1 | ForEach-Object {
Import-Module $_.FullName
}
$SnipeItAliases= @{'Get-Asset' = 'Get-SnipeItAsset';
'Get-AssetMaintenance' = 'Get-SnipeItAssetMaintenance';
'Get-Category' = 'Get-SnipeItCategory';
'Get-Company' = 'Get-SnipeItCompany';
'Get-Component' = 'Get-SnipeItCompany';
'Get-CustomField' = 'Get-SnipeItCustomField';
'Get-Department' = 'Get-SnipeItDepartment';
'Get-Fieldset' = 'Get-SnipeItFieldset';
'Get-Manufacturer' = 'Get-SnipeItManufacturer';
'Get-Model' = 'Get-SnipeItModel';
'Get-Status' = 'Get-SnipeItStatus';
'Get-Supplier' = 'Get-SnipeItSupplier';
'Get-User' = 'Get-SnipeItUser';
'New-Asset' = 'New-SnipeItAsset';
'New-AssetMaintenance' = 'New-SnipeItAssetMaintenance';
'New-Category' = 'New-SnipeItCategory';
'New-Component' = 'New-SnipeItComponent';
'New-CustomField' = 'New-SnipeItCustomField';
'New-Department' = 'New-SnipeItDepartment';
'New-License' = 'New-SnipeItLicense';
'Set-License' = 'Set-SnipeItLicense';
'New-Location' = 'New-SnipeItLocation';
'New-Manufacturer' = 'New-SnipeItManufacturer';
'New-Model' = 'New-SnipeItModel';
'New-User' = 'New-SnipeItUser';
'Set-Asset' = 'Set-SnipeItAsset';
'Set-AssetOwner' = 'Set-SnipeItAssetOwner';
'Set-Component' = 'Set-SnipeItComponent';
'Set-Model' = 'Set-SnipeItModel';
'Set-Info' = 'Set-SnipeItInfo';
'Set-User' = 'Set-SnipeItUser';
'New-Accessory' = 'New-SnipeItAccessory';
'Set-Accessory' = 'Set-SnipeItAccessory';
'Get-Accessory' = 'Get-SnipeItAccessory';
'Remove-Asset' = 'Remove-SnipeItAsset';
'Remove-User' = 'Remove-SnipeItUser';}
#Create unprefixed aliases
Set-SnipeItAlias

View file

@ -122,3 +122,6 @@ Updates Model on Snipe-it asset system
### [Set-SnipeItUser](Set-SnipeItUser.md)
Creates a new user
### [Update-SnipeItAlias](Update-SnipeItAlias.md)
{{ Fill in the Synopsis }}

View file

@ -0,0 +1,91 @@
---
external help file: SnipeItPS-help.xml
Module Name: SnipeitPS
online version:
schema: 2.0.0
---
# Update-SnipeItAlias
## SYNOPSIS
{{ Fill in the Synopsis }}
## SYNTAX
```
Update-SnipeItAlias [-String] <String[]> [-WhatIf] [-Confirm] [<CommonParameters>]
```
## DESCRIPTION
{{ Fill in the Description }}
## EXAMPLES
### Example 1
```powershell
PS C:\> {{ Add example code here }}
```
{{ Add example description here }}
## PARAMETERS
### -String
{{ Fill String Description }}
```yaml
Type: String[]
Parameter Sets: (All)
Aliases:
Required: True
Position: 0
Default value: None
Accept pipeline input: True (ByValue)
Accept wildcard characters: False
```
### -Confirm
Prompts you for confirmation before running the cmdlet.
```yaml
Type: SwitchParameter
Parameter Sets: (All)
Aliases: cf
Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### -WhatIf
Shows what would happen if the cmdlet runs.
The cmdlet is not run.
```yaml
Type: SwitchParameter
Parameter Sets: (All)
Aliases: wi
Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### CommonParameters
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216).
## INPUTS
### System.String[]
## OUTPUTS
### System.Object
## NOTES
## RELATED LINKS