From f83d64b402ce9faf803db11e24291a51aea0b8e1 Mon Sep 17 00:00:00 2001 From: Michael Arranaga Date: Wed, 7 Aug 2019 13:00:41 -0500 Subject: [PATCH] Added (#23) --- .../Remove-ePOComputer/References/12321.html | 2 + .../Remove-ePOComputer/References/12345.html | 2 + .../Remove-ePOComputer/References/54321.html | 2 + .../References/Computer1.html | 58 ++++++++++++ .../References/Computer2.html | Bin 0 -> 6068 bytes .../References/Computer3.html | 58 ++++++++++++ ...move-ePOComputer.Deletes One Computer.psd1 | 8 ++ ...ove-ePOComputer.Deletes Two Computers.psd1 | 11 +++ ...Computer.Fails To Delete One Computer.psd1 | 8 ++ Tests/Public/Remove-ePOComputer.Tests.ps1 | 86 +++++++++++++++++ ePOwerShell/Public/Remove-ePOComputer.ps1 | 87 ++++++++++++++++++ 11 files changed, 322 insertions(+) create mode 100644 Examples/Public/Remove-ePOComputer/References/12321.html create mode 100644 Examples/Public/Remove-ePOComputer/References/12345.html create mode 100644 Examples/Public/Remove-ePOComputer/References/54321.html create mode 100644 Examples/Public/Remove-ePOComputer/References/Computer1.html create mode 100644 Examples/Public/Remove-ePOComputer/References/Computer2.html create mode 100644 Examples/Public/Remove-ePOComputer/References/Computer3.html create mode 100644 Examples/Public/Remove-ePOComputer/Remove-ePOComputer.Deletes One Computer.psd1 create mode 100644 Examples/Public/Remove-ePOComputer/Remove-ePOComputer.Deletes Two Computers.psd1 create mode 100644 Examples/Public/Remove-ePOComputer/Remove-ePOComputer.Fails To Delete One Computer.psd1 create mode 100644 Tests/Public/Remove-ePOComputer.Tests.ps1 create mode 100644 ePOwerShell/Public/Remove-ePOComputer.ps1 diff --git a/Examples/Public/Remove-ePOComputer/References/12321.html b/Examples/Public/Remove-ePOComputer/References/12321.html new file mode 100644 index 0000000..0f556f1 --- /dev/null +++ b/Examples/Public/Remove-ePOComputer/References/12321.html @@ -0,0 +1,2 @@ +OK: +1 \ No newline at end of file diff --git a/Examples/Public/Remove-ePOComputer/References/12345.html b/Examples/Public/Remove-ePOComputer/References/12345.html new file mode 100644 index 0000000..8f9b20d --- /dev/null +++ b/Examples/Public/Remove-ePOComputer/References/12345.html @@ -0,0 +1,2 @@ +OK: +0 \ No newline at end of file diff --git a/Examples/Public/Remove-ePOComputer/References/54321.html b/Examples/Public/Remove-ePOComputer/References/54321.html new file mode 100644 index 0000000..8f9b20d --- /dev/null +++ b/Examples/Public/Remove-ePOComputer/References/54321.html @@ -0,0 +1,2 @@ +OK: +0 \ No newline at end of file diff --git a/Examples/Public/Remove-ePOComputer/References/Computer1.html b/Examples/Public/Remove-ePOComputer/References/Computer1.html new file mode 100644 index 0000000..c34242e --- /dev/null +++ b/Examples/Public/Remove-ePOComputer/References/Computer1.html @@ -0,0 +1,58 @@ +OK: +[ + { + "EPOComputerProperties.ParentID": 12345, + "EPOComputerProperties.ComputerName": "Computer1", + "EPOComputerProperties.Description": null, + "EPOComputerProperties.SystemDescription": "N/A", + "EPOComputerProperties.TimeZone": "Central Standard Time", + "EPOComputerProperties.DefaultLangID": "0409", + "EPOComputerProperties.UserName": "Username1", + "EPOComputerProperties.DomainName": "Domain", + "EPOComputerProperties.IPHostName": "Computer1.domain.com", + "EPOComputerProperties.IPV6": "0:0:0:0:0:FFFF:A78:21F1", + "EPOComputerProperties.IPAddress": "10.120.45.123", + "EPOComputerProperties.IPSubnet": "0:0:0:0:0:FFFF:A78:2100", + "EPOComputerProperties.IPSubnetMask": "0:0:0:0:0:FFFF:FFFF:FF00", + "EPOComputerProperties.IPV4x": -1971838479, + "EPOComputerProperties.IPXAddress": "N/A", + "EPOComputerProperties.SubnetAddress": "", + "EPOComputerProperties.SubnetMask": "", + "EPOComputerProperties.NetAddress": "509A4C567EBB", + "EPOComputerProperties.OSType": "Windows 10", + "EPOComputerProperties.OSVersion": "10.0", + "EPOComputerProperties.OSServicePackVer": "", + "EPOComputerProperties.OSBuildNum": 16299, + "EPOComputerProperties.OSPlatform": "Workstation", + "EPOComputerProperties.OSOEMID": "00329-00000-00003-AA862", + "EPOComputerProperties.CPUType": "Intel(R) Core(TM) i7-7700 CPU @ 3.60GHz", + "EPOComputerProperties.CPUSpeed": 3600, + "EPOComputerProperties.NumOfCPU": 8, + "EPOComputerProperties.CPUSerialNum": "N/A", + "EPOComputerProperties.TotalPhysicalMemory": 34237841408, + "EPOComputerProperties.FreeMemory": 6870249472, + "EPOComputerProperties.FreeDiskSpace": 413878, + "EPOComputerProperties.TotalDiskSpace": 720157, + "EPOComputerProperties.IsPortable": 0, + "EPOComputerProperties.Vdi": 0, + "EPOComputerProperties.OSBitMode": 1, + "EPOComputerProperties.LastAgentHandler": 2, + "EPOComputerProperties.UserProperty1": "", + "EPOComputerProperties.UserProperty2": "", + "EPOComputerProperties.UserProperty3": "", + "EPOComputerProperties.UserProperty4": "", + "EPOComputerProperties.UserProperty5": "", + "EPOComputerProperties.UserProperty6": "", + "EPOComputerProperties.UserProperty7": "", + "EPOComputerProperties.UserProperty8": "", + "EPOComputerProperties.SysvolFreeSpace": 36732, + "EPOComputerProperties.SysvolTotalSpace": 243219, + "EPOLeafNode.Tags": "Windows, Workstation, Tag4", + "EPOLeafNode.ExcludedTags": "", + "EPOLeafNode.LastUpdate": "2018-08-08T08:40:23-05:00", + "EPOLeafNode.ManagedState": 1, + "EPOLeafNode.AgentGUID": "20f1477f-ff4d-4c6f-915d-9984d25fd65b", + "EPOLeafNode.AgentVersion": "5.0.6.220", + "EPOBranchNode.AutoID": 2 + } +] \ No newline at end of file diff --git a/Examples/Public/Remove-ePOComputer/References/Computer2.html b/Examples/Public/Remove-ePOComputer/References/Computer2.html new file mode 100644 index 0000000000000000000000000000000000000000..37cb8b11715b2a1eb65662c76d604ca290dff79c GIT binary patch literal 6068 zcmchbTTdHT6ovP5rTz!xr&J1be7AY336P)!jA|fNt3DXpNv#-&j7dwY{`I!s+S6gi zj4h|t4zdnDJ~N!PFKb`U`TMWFy|!KZ+P<=%{O^{fpFfsX3%ju^`)GHz zu!+rCo3rxK=L?(ieQP_c4q2brHE$i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d00001 diff --git a/Examples/Public/Remove-ePOComputer/References/Computer3.html b/Examples/Public/Remove-ePOComputer/References/Computer3.html new file mode 100644 index 0000000..6d03614 --- /dev/null +++ b/Examples/Public/Remove-ePOComputer/References/Computer3.html @@ -0,0 +1,58 @@ +OK: +[ + { + "EPOComputerProperties.ParentID": 12321, + "EPOComputerProperties.ComputerName": "Computer3", + "EPOComputerProperties.Description": null, + "EPOComputerProperties.SystemDescription": "N/A", + "EPOComputerProperties.TimeZone": "Central Standard Time", + "EPOComputerProperties.DefaultLangID": "0409", + "EPOComputerProperties.UserName": "Username3", + "EPOComputerProperties.DomainName": "Domain", + "EPOComputerProperties.IPHostName": "Computer3.domain.com", + "EPOComputerProperties.IPV6": "0:0:0:0:0:FFFF:A78:21F1", + "EPOComputerProperties.IPAddress": "10.120.45.125", + "EPOComputerProperties.IPSubnet": "0:0:0:0:0:FFFF:A78:2100", + "EPOComputerProperties.IPSubnetMask": "0:0:0:0:0:FFFF:FFFF:FF00", + "EPOComputerProperties.IPV4x": -1971838479, + "EPOComputerProperties.IPXAddress": "N/A", + "EPOComputerProperties.SubnetAddress": "", + "EPOComputerProperties.SubnetMask": "", + "EPOComputerProperties.NetAddress": "509A4C567EA9", + "EPOComputerProperties.OSType": "Windows 10", + "EPOComputerProperties.OSVersion": "10.0", + "EPOComputerProperties.OSServicePackVer": "", + "EPOComputerProperties.OSBuildNum": 16299, + "EPOComputerProperties.OSPlatform": "Workstation", + "EPOComputerProperties.OSOEMID": "00329-00000-00003-AA862", + "EPOComputerProperties.CPUType": "Intel(R) Core(TM) i7-7700 CPU @ 3.60GHz", + "EPOComputerProperties.CPUSpeed": 3600, + "EPOComputerProperties.NumOfCPU": 8, + "EPOComputerProperties.CPUSerialNum": "N/A", + "EPOComputerProperties.TotalPhysicalMemory": 34237841408, + "EPOComputerProperties.FreeMemory": 6870249472, + "EPOComputerProperties.FreeDiskSpace": 413878, + "EPOComputerProperties.TotalDiskSpace": 720157, + "EPOComputerProperties.IsPortable": 0, + "EPOComputerProperties.Vdi": 0, + "EPOComputerProperties.OSBitMode": 1, + "EPOComputerProperties.LastAgentHandler": 2, + "EPOComputerProperties.UserProperty1": "", + "EPOComputerProperties.UserProperty2": "", + "EPOComputerProperties.UserProperty3": "", + "EPOComputerProperties.UserProperty4": "", + "EPOComputerProperties.UserProperty5": "", + "EPOComputerProperties.UserProperty6": "", + "EPOComputerProperties.UserProperty7": "", + "EPOComputerProperties.UserProperty8": "", + "EPOComputerProperties.SysvolFreeSpace": 36732, + "EPOComputerProperties.SysvolTotalSpace": 243219, + "EPOLeafNode.Tags": "Windows, Workstation", + "EPOLeafNode.ExcludedTags": "", + "EPOLeafNode.LastUpdate": "2018-08-08T08:40:23-05:00", + "EPOLeafNode.ManagedState": 1, + "EPOLeafNode.AgentGUID": "69aba147-7c36-419f-858d-98e724dad78c", + "EPOLeafNode.AgentVersion": "5.0.6.220", + "EPOBranchNode.AutoID": 2 + } +] \ No newline at end of file diff --git a/Examples/Public/Remove-ePOComputer/Remove-ePOComputer.Deletes One Computer.psd1 b/Examples/Public/Remove-ePOComputer/Remove-ePOComputer.Deletes One Computer.psd1 new file mode 100644 index 0000000..c79c516 --- /dev/null +++ b/Examples/Public/Remove-ePOComputer/Remove-ePOComputer.Deletes One Computer.psd1 @@ -0,0 +1,8 @@ +@{ + Parameters = @{ + ComputerName = 'Computer1' + } + Output = @{ + Type = 'System.Void' + } +} \ No newline at end of file diff --git a/Examples/Public/Remove-ePOComputer/Remove-ePOComputer.Deletes Two Computers.psd1 b/Examples/Public/Remove-ePOComputer/Remove-ePOComputer.Deletes Two Computers.psd1 new file mode 100644 index 0000000..fe989a4 --- /dev/null +++ b/Examples/Public/Remove-ePOComputer/Remove-ePOComputer.Deletes Two Computers.psd1 @@ -0,0 +1,11 @@ +@{ + Parameters = @{ + ComputerName = @( + 'Computer1', + 'Computer2' + ) + } + Output = @{ + Type = 'System.Void' + } +} \ No newline at end of file diff --git a/Examples/Public/Remove-ePOComputer/Remove-ePOComputer.Fails To Delete One Computer.psd1 b/Examples/Public/Remove-ePOComputer/Remove-ePOComputer.Fails To Delete One Computer.psd1 new file mode 100644 index 0000000..881751f --- /dev/null +++ b/Examples/Public/Remove-ePOComputer/Remove-ePOComputer.Fails To Delete One Computer.psd1 @@ -0,0 +1,8 @@ +@{ + Parameters = @{ + ComputerName = 'Computer3' + } + Output = @{ + Throws = $True + } +} \ No newline at end of file diff --git a/Tests/Public/Remove-ePOComputer.Tests.ps1 b/Tests/Public/Remove-ePOComputer.Tests.ps1 new file mode 100644 index 0000000..4da6156 --- /dev/null +++ b/Tests/Public/Remove-ePOComputer.Tests.ps1 @@ -0,0 +1,86 @@ +[System.String] $ProjectDirectoryName = 'ePOwerShell' +[System.String] $FunctionType = 'Public' +[IO.FileInfo] $PesterFile = [IO.FileInfo] ([System.String] (Resolve-Path -Path $MyInvocation.MyCommand.Path)) +[System.String] $FunctionName = $PesterFile.Name.Split('.')[0] +[IO.DirectoryInfo] $ProjectRoot = Split-Path -Parent $PesterFile.Directory + +while (-not ($ProjectRoot.Name -eq $ProjectDirectoryName)) { + $ProjectRoot = Split-Path -Parent $ProjectRoot.FullName +} + +[IO.DirectoryInfo] $ExampleDirectory = Join-Path (Join-Path -Path $ProjectRoot -ChildPath 'Examples' -Resolve) -ChildPath $FunctionType -Resolve +[IO.DirectoryInfo] $ExampleDirectory = Join-Path $ExampleDirectory.FullName -ChildPath $FunctionName -Resolve +[IO.DirectoryInfo] $Global:ReferenceDirectory = Join-Path $ExampleDirectory.FullName -ChildPath 'References' -Resolve + +$Examples = Get-ChildItem $ExampleDirectory -Filter "*.psd1" -File + +$Tests = foreach ($Example in $Examples) { + [hashtable] $Test = @{ + Name = $Example.BaseName.Split('.')[1] + } + + Write-Verbose "Test: $($Test | ConvertTo-Json)" + + foreach ($ExampleData in (Import-PowerShellDataFile -LiteralPath $Example.FullName).GetEnumerator()) { + $Test.Add($ExampleData.Name, $ExampleData.Value) | Out-Null + } + + Write-Verbose "Test: $($Test | ConvertTo-Json)" + Write-Output $Test +} + +Describe $FunctionName { + foreach ($Global:Test in $Tests) { + InModuleScope ePOwerShell { + Mock Invoke-ePORequest { + if (-not ($ResultsFile = Get-ChildItem $ReferenceDirectory.FullName -Filter ('{0}.html' -f $Query.ids))) { + Throw "Error 1: Invalid computername" + } + + $Results = (Get-Content $ResultsFile.FullName | Out-String).Substring(3).Trim() | ConvertFrom-Json + Write-Output $Results + } + + Mock Get-ePOComputer { + if (-not ($ComputerFile = Get-ChildItem $ReferenceDirectory.FullName -Filter ('{0}.html' -f $Computer))) { + Throw "Error 1: Invalid computername" + } + + $Computer = ConvertTo-ePOComputer ((Get-Content $ComputerFile.FullName | Out-String).Substring(3).Trim() | ConvertFrom-Json) + Write-Output $Computer + } + + Mock Write-Warning { + Write-Debug $Message + } + + Mock Write-Error { + Throw $Message + } + + Remove-Variable -Scope 'Script' -Name 'RequestResponse' -Force -ErrorAction SilentlyContinue + + Context $Test.Name { + [hashtable] $parameters = $Test.Parameters + + if ($Test.Output.Throws) { + It "Remove-ePOComputer Throws" { + { $script:RequestResponse = Remove-ePOComputer @parameters -Confirm:$False} | Should Throw + } + continue + } + + It "Remove-ePOComputer" { + { $script:RequestResponse = Remove-ePOComputer @parameters -Confirm:$False } | Should Not Throw + } + + It "Output Type: $($Test.Output.Type)" { + $script:RequestResponse | Should BeNullOrEmpty + } + } + } + } + + Remove-Variable -Scope 'Global' -Name 'Test' -Force -ErrorAction SilentlyContinue + Remove-Variable -Scope 'Global' -Name 'ReferenceDirectory' -Force -ErrorAction SilentlyContinue +} \ No newline at end of file diff --git a/ePOwerShell/Public/Remove-ePOComputer.ps1 b/ePOwerShell/Public/Remove-ePOComputer.ps1 new file mode 100644 index 0000000..8a71d4b --- /dev/null +++ b/ePOwerShell/Public/Remove-ePOComputer.ps1 @@ -0,0 +1,87 @@ +<# + .SYNOPSIS + Removes computers managed in ePO + + .DESCRIPTION + Using the supplied ComputerName(s), we can remove the computer specified from ePO + + .EXAMPLE + Remove-ePOComputer -Computer $Computer + + Remove a single tag on a single computer + + .EXAMPLE + Remove-ePOComputer -Computer @(Computer1, Computer2) + + Remove one tag on two computers + + .EXAMPLE + Get-ePOComputer My-Computer123 | Remove-ePOComputer + + Remove a single computer through the pipeline + + .PARAMETER Computer + Specifies the name of the computer managed by ePO to be removed. This can be provided by: + + * An ePOComputer object + * A computer name + + This parameter can be provided through the pipeline + + .OUTPUTS + None +#> + +function Remove-ePOComputer { + [CmdletBinding(SupportsShouldProcess = $True, ConfirmImpact = "High")] + param ( + [Parameter(Mandatory = $True, Position = 0, ValueFromPipeline = $True)] + [Alias('ComputerName', 'cn', 'Name')] + $Computer + ) + + begin { + try { + $Request = @{ + Name = 'system.delete' + Query = @{ + ids = '' + } + } + } catch { + Write-Information $_ -Tags Exception + Throw $_ + } + } + + process { + try { + foreach ($Comp in $Computer) { + if ($Comp -is [ePOComputer]) { + Write-Debug ('Specified computer is an ePOComputer: -f {0}' -f $Comp) + } else { + Write-Debug ('Specified computer is a string: -f {0}' -f $Comp) + $Comp = Get-ePOComputer $Comp + } + + foreach ($C in $Comp) { + Write-Verbose ('Computer Name: {0}' -f $C.ComputerName) + Write-Verbose ('Computer ID: {0}' -f $C.ParentID) + + $Request.Query.ids = $C.ParentID + if ($PSCmdlet.ShouldProcess("Remove ePO computer: $($C.ComputerName)")) { + $Result = Invoke-ePORequest @Request + + if ($Result -eq 0) { + Write-Verbose ('Deleted computer: {0}' -f $C.ComputerName) + } else { + Throw ('Unknown response while deleting computer {0} from ePO: {1}' -f $C.ComputerName, $Result) + } + } + } + } + } catch { + Write-Error $_ + } + } +} \ No newline at end of file