NN.MrfkCommands.psm1
#Region './Public/Get-MrfkAdmCreds.ps1' 0 function Get-MrfkAdmCreds { param ( [string]$admCredsPath = "$env:USERPROFILE\.creds\MRFK\adm_creds.xml" ) if (!(Test-Path $admCredsPath)) { New-MrfkAdmCreds } Import-Clixml $admCredsPath } #EndRegion './Public/Get-MrfkAdmCreds.ps1' 12 #Region './Public/Get-MrfkComputerInfo.ps1' 0 function Get-MrfkComputerInfo { [CmdletBinding()] param ( [Parameter(Mandatory,ParameterSetName="Get computer by hostname",ValueFromPipeline,Position=0)] [string]$Hostname, [Parameter(Mandatory,ParameterSetName="Get computers by username")][string]$Username, [string]$MECMNameSpace = "root/SMS/site_PS1", [string]$MECMHost = "sccm-ps.intern.mrfylke.no", [string]$DC = "dc01.intern.mrfylke.no" ) begin { try { $null = Get-ADUser -Filter "Name -eq 0" } catch [System.Management.Automation.CommandNotFoundException] { Write-Error -ErrorAction Stop -Message @" Please install RSAT before running this function. You can install RSAT by following this guide: https://github.com/NorskNoobing/NN.MrfkCommands#prerequisites "@ } $splat = @{ "Credential" = Get-MrfkAdmCreds "ComputerName" = $MECMHost "ErrorAction" = "Stop" } $CimSession = New-CimSession @splat } process { $ComputerExportArr = New-Object -TypeName System.Collections.ArrayList $Notes = New-Object -TypeName System.Collections.ArrayList switch ($PsCmdlet.ParameterSetName) { "Get computer by hostname" { [array]$HostnameArr = $Hostname } "Get computers by username" { $splat = @{ "Query" = "Select * from SMS_R_System where LastLogonUserName = `"$Username`"" "Namespace" = $MECMNameSpace "CimSession" = $CimSession } [array]$HostnameArr = (Get-CimInstance @splat).Name } } $HostnameArr.ForEach({ $splat = @{ "Query" = "Select * from SMS_R_System where name = `"$_`"" "Namespace" = $MECMNameSpace "CimSession" = $CimSession } $MecmComputer = Get-CimInstance @splat if ($MecmComputer) { $splat = @{ "Query" = @" select distinct SMS_G_System_PROCESSOR.* from SMS_R_System inner join SMS_G_System_PROCESSOR on SMS_G_System_PROCESSOR.ResourceID = SMS_R_System.ResourceId where ResourceId = $($MecmComputer.ResourceId) "@ "Namespace" = $MECMNameSpace "CimSession" = $CimSession } $CPUInfo = Get-CimInstance @splat $splat = @{ "Query" = @" Select * from SMS_G_System_Computer_System_Product where ResourceId = $($MecmComputer.ResourceId) "@ "Namespace" = $MECMNameSpace "CimSession" = $CimSession } $ModelInfo = Get-CimInstance @splat } else { $Notes.Add("Couldn't find `"$Hostname`" in MECM.") } try { $ADComputer = Get-ADComputer $_ $splat = @{ "Filter" = {objectclass -eq "msFVE-RecoveryInformation"} "SearchBase" = $ADComputer.DistinguishedName "Properties" = "msFVE-RecoveryPassword" "Credential" = Get-MrfkAdmCreds "Server" = $DC } $BitlockerRecoveryKeys = (Get-ADObject @splat)."msFVE-RecoveryPassword" } catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException] { $Notes.Add("Couldn't find `"$Hostname`" in AD.") } if ($MecmComputer.agentname) { $HeartbeatIndex = $MecmComputer.agentname.IndexOf("Heartbeat Discovery") $LastHeartbeat = $MecmComputer.agenttime[$HeartbeatIndex] } $null = $ComputerExportArr.Add( [PSCustomObject]@{ "Hostname" = $_ "LastLoggedOnUser" = $MecmComputer.LastLogonUserName "MACAddresses" = $MecmComputer.MACAddresses "Model" = $ModelInfo.Name "CPUName" = $CPUInfo.Name "SN" = $ModelInfo.IdentifyingNumber "LastHeartbeat" = $LastHeartbeat "BitlockerRecoveryKeys" = $BitlockerRecoveryKeys "Notes" = $Notes } ) }) $ComputerExportArr } } #EndRegion './Public/Get-MrfkComputerInfo.ps1' 123 #Region './Public/Get-MrfkFreeHostnames.ps1' 0 function Get-MrfkFreeHostnames { param ( [Parameter(Mandatory)][ValidateSet("LT","PC","TB")][string]$Prefix, [Parameter(Mandatory)][string]$LocationCode, [Parameter(Mandatory)][int]$Digits, [int]$Count, [string]$MECMNameSpace = "root/SMS/site_PS1", [string]$MECMHost = "sccm-ps.intern.mrfylke.no" ) $CimSession = New-CimSession -Credential (Get-MrfkAdmCreds) -ComputerName $MECMHost -ErrorAction Stop $AllMecmComputers = Get-CimInstance -Namespace $MECMNameSpace -CimSession $CimSession -Query @" Select * from SMS_R_System "@ $Filter = "$Prefix-$LocationCode-" $PCNumArr = $AllMecmComputers.where({ ($_.Name -like "$Filter*") -and ( $_.Name -like ($Filter + (( $_.Name.Replace("$Filter","","OrdinalIgnoreCase") -replace "^0+","" ).PadLeft($Digits,'0'))) ) }).Name if ($PCNumArr) { $PCNumArr = $PCNumArr.Replace("$Filter","","OrdinalIgnoreCase") -replace "^0+","" | Sort-Object } $FreePCNumArr = (1..("9" * $Digits)).where({$_ -notin $PCNumArr}) if ($Count) { $FreePCNumArr = $FreePCNumArr | Select-Object -First $Count } $FreePCNumArr.ForEach({ $Num = ([string]$_).PadLeft($Digits,'0') "$Filter$Num" }) } #EndRegion './Public/Get-MrfkFreeHostnames.ps1' 40 #Region './Public/Get-MrfkUserInfo.ps1' 0 function Get-MrfkUserInfo { [CmdletBinding()] param ( [Parameter(ParameterSetName="username")][string]$Username, [Parameter(ParameterSetName="displayname")][string]$DisplayName, [Parameter(ParameterSetName="mobilephone")][string]$MobilePhone, [switch]$IncludeComputerInfo, [switch]$ExpandComputerInfo ) begin { try { $null = Get-ADUser -Filter "Name -eq 0" } catch [System.Management.Automation.CommandNotFoundException] { Write-Error -ErrorAction Stop -Message @" Please install RSAT before running this function. You can install RSAT by following this guide: https://github.com/NorskNoobing/NN.MrfkCommands#prerequisites "@ } } process { switch ($PsCmdlet.ParameterSetName) { "username" { $filter = "SamAccountName -like `"$Username`"" } "displayname" { $filter = "DisplayName -like `"$DisplayName`"" } "mobilephone" { $filter = "MobilePhone -like `"$MobilePhone`"" } } #Get userinfo of the ADusers $ADUser = Get-ADUser -filter $filter -Properties MobilePhone,DisplayName | Select-Object @( "DisplayName","Name","SamAccountName","MobilePhone", "UserPrincipalName","Enabled","DistinguishedName" ) #Pick an ADUser if we get multiple hits on the search query if ($ADUser -is [array]) { $splat = @{ "Title" = "Found multiple hits on the input. Please select an user." "OutputMode" = "Single" } $ADUser = $ADUser | Out-GridView @splat } if (!$ADUser) { Write-Error -ErrorAction "Stop" -Message "Please select a user before continuing." } if ($IncludeComputerInfo) { $ComputerExportArr = Get-MrfkComputerInfo -Username $ADUser.SamAccountName if (!$ExpandComputerInfo) { $ADUser | Add-Member -MemberType NoteProperty -Name "Computers" -Value $ComputerExportArr } } #Post output $ADUser if ($ExpandComputerInfo) { $ComputerExportArr } } } #EndRegion './Public/Get-MrfkUserInfo.ps1' 69 #Region './Public/New-MrfkAdmCreds.ps1' 0 function New-MrfkAdmCreds { param ( [string]$admCredsPath = "$env:USERPROFILE\.creds\MRFK\adm_creds.xml" ) #Create parent folders for the file $admCredsDir = $admCredsPath.Substring(0, $admCredsPath.lastIndexOf('\')) if (!(Test-Path $admCredsDir)) { $null = New-Item -ItemType Directory $admCredsDir } #Create adm_creds file Get-Credential -Message "Enter your mrfk admin credentials" | Export-Clixml $admCredsPath } #EndRegion './Public/New-MrfkAdmCreds.ps1' 15 #Region './Public/Remove-MrfkComputer.ps1' 0 function Remove-MrfkComputer { [CmdletBinding()] param ( [Parameter(Mandatory,ValueFromPipeline,Position=0)][string]$Hostname, [string]$MECMNameSpace = "root/SMS/site_PS1", [string]$MECMHost = "sccm-ps.intern.mrfylke.no" ) begin { try { $null = Get-ADUser -Filter "Name -eq 0" } catch [System.Management.Automation.CommandNotFoundException] { Write-Error -ErrorAction Stop -Message @" Please install RSAT before running this function. You can install RSAT by following this guide: https://github.com/NorskNoobing/NN.MrfkCommands#prerequisites "@ } $CimSession = New-CimSession -ComputerName $MECMHost -Credential (Get-MrfkAdmCreds) } process { $MECMComputer = Get-CimInstance -CimSession $CimSession -Namespace $MECMNameSpace -Query @" Select * from SMS_R_System where name = `"$Hostname`" "@ if ($MECMComputer) { Remove-CimInstance -CimInstance $MECMComputer } else { Write-Warning -Message "Couldn't find any computer in MECM with the name `"$Hostname`"" } try { $ADComputer = Get-ADComputer $Hostname } catch { Write-Warning -Message "Couldn't find any computer in AD with the name `"$Hostname`"" } if ($ADComputer) { (Get-ADObject -Filter * -SearchBase $ADComputer.DistinguishedName).ForEach({ if($_.DistinguishedName -ne $ADComputer.DistinguishedName) { Remove-ADObject $_.DistinguishedName -Credential (Get-MrfkAdmCreds) -Confirm:$false } }) Remove-ADComputer -Identity $ADComputer.DistinguishedName -Credential (Get-MrfkAdmCreds) -Confirm:$false } } } #EndRegion './Public/Remove-MrfkComputer.ps1' 51 |