From bbfdb5d61f58f01ff3de2cea7f04eeab4b7ff805 Mon Sep 17 00:00:00 2001 From: Alex Kup <116945542+Lifailon@users.noreply.github.com> Date: Tue, 12 Dec 2023 16:39:28 +0300 Subject: [PATCH] Add modules from WinAPI (process managment and cim hardware) --- Scripts/Find-Process.psm1 | 18 ++++++ Scripts/Get-Hardware.psm1 | 93 +++++++++++++++++++++++++++++ Scripts/Get-IOps.psm1 | 6 ++ Scripts/Get-LD.psm1 | 17 ++++++ Scripts/Get-MemorySize.psm1 | 15 +++++ Scripts/Get-MemorySlots.psm1 | 16 +++++ Scripts/Get-NetAdapter.psm1 | 8 +++ Scripts/Get-PD.psm1 | 14 +++++ Scripts/Get-Performance.psm1 | 12 ++++ Scripts/Get-ProcessDescription.psm1 | 29 +++++++++ Scripts/Get-VideoCard.psm1 | 15 +++++ 11 files changed, 243 insertions(+) create mode 100644 Scripts/Find-Process.psm1 create mode 100644 Scripts/Get-Hardware.psm1 create mode 100644 Scripts/Get-IOps.psm1 create mode 100644 Scripts/Get-LD.psm1 create mode 100644 Scripts/Get-MemorySize.psm1 create mode 100644 Scripts/Get-MemorySlots.psm1 create mode 100644 Scripts/Get-NetAdapter.psm1 create mode 100644 Scripts/Get-PD.psm1 create mode 100644 Scripts/Get-Performance.psm1 create mode 100644 Scripts/Get-ProcessDescription.psm1 create mode 100644 Scripts/Get-VideoCard.psm1 diff --git a/Scripts/Find-Process.psm1 b/Scripts/Find-Process.psm1 new file mode 100644 index 0000000..770e89e --- /dev/null +++ b/Scripts/Find-Process.psm1 @@ -0,0 +1,18 @@ +function Find-Process { + param ( + $ProcessName + ) + $ProcessPath = (Get-ChildItem "C:\Program Files" | Where-Object Name -match $ProcessName).FullName + if ($null -eq $ProcessPath) { + $ProcessPath = (Get-ChildItem "C:\Program Files (x86)" | Where-Object Name -match $ProcessName).FullName + } + if ($null -eq $ProcessPath) { + $ProcessPath = (Get-ChildItem "C:\Users\lifailon\AppData\Roaming" | Where-Object Name -match $ProcessName).FullName + } + $ProcessNameExec = "$ProcessName"+".exe" + (Get-ChildItem $ProcessPath -Recurse | Where-Object Name -eq $ProcessNameExec).FullName +} + +# Find-Process qbittorrent # C:\Program Files\qBittorrent\qbittorrent.exe +# Find-Process nmap # C:\Program Files (x86)\Nmap\nmap.exe +# Find-Process telegram # C:\Users\lifailon\AppData\Roaming\Telegram Desktop\Telegram.exe \ No newline at end of file diff --git a/Scripts/Get-Hardware.psm1 b/Scripts/Get-Hardware.psm1 new file mode 100644 index 0000000..193b4da --- /dev/null +++ b/Scripts/Get-Hardware.psm1 @@ -0,0 +1,93 @@ +function Get-Hardware { + param ( + $ComputerName, + $Port = 8443, + $User = "rest", + $Pass = "api" + ) + if ($null -eq $ComputerName) { + $Collection = New-Object System.Collections.Generic.List[System.Object] + $SYS = Get-CimInstance Win32_ComputerSystem + $BootTime = Get-CimInstance -ComputerName $srv Win32_OperatingSystem | Select-Object LocalDateTime,LastBootUpTime + $Uptime = ([string]($BootTime.LocalDateTime - $BootTime.LastBootUpTime) -split ":")[0,1] -join ":" + $OS = Get-CimInstance Win32_OperatingSystem + $BB = Get-CimInstance Win32_BaseBoard + $BBv = $BB.Manufacturer+" "+$BB.Product+" "+$BB.Version + $CPU = Get-CimInstance Win32_Processor | Select-Object Name, + @{Label="Core"; Expression={$_.NumberOfCores}}, + @{Label="Thread"; Expression={$_.NumberOfLogicalProcessors}} + $CPU_Use_Proc = [string]((Get-CimInstance Win32_PerfFormattedData_PerfOS_Processor -ErrorAction Ignore | + Where-Object name -eq "_Total").PercentProcessorTime)+" %" + $GetProcess = Get-Process + $Process_Count = $GetProcess.Count + $Threads_Count = $GetProcess.Threads.Count + $Handles_Count = ($GetProcess.Handles | Measure-Object -Sum).Sum + $ws = ((($GetProcess).WorkingSet | Measure-Object -Sum).Sum/1gb).ToString("0.00 GB") + $pm = ((($GetProcess).PM | Measure-Object -Sum).Sum/1gb).ToString("0.00 GB") + $Memory = Get-CimInstance Win32_OperatingSystem + $MemUse = $Memory.TotalVisibleMemorySize - $Memory.FreePhysicalMemory + $MEM = Get-CimInstance Win32_PhysicalMemory | Select-Object Manufacturer,PartNumber, + ConfiguredClockSpeed,@{Label="Memory"; Expression={[string]($_.Capacity/1Mb)}} + $MEMs = $MEM.Memory | Measure-Object -Sum + $PhysicalDisk = Get-CimInstance Win32_DiskDrive | Select-Object Model, + @{Label="Size"; Expression={[int]($_.Size/1Gb)}} + $PDs = $PhysicalDisk.Size | Measure-Object -Sum + $LogicalDisk = Get-CimInstance Win32_logicalDisk | Where-Object {$null -ne $_.Size} | Select-Object @{ + Label="Value"; Expression={$_.DeviceID}}, @{Label="AllSize"; Expression={ + ([int]($_.Size/1Gb))}},@{Label="FreeSize"; Expression={ + ([int]($_.FreeSpace/1Gb))}}, @{Label="Free%"; Expression={ + [string]([int]($_.FreeSpace/$_.Size*100))+" %"}} + $LDs = $LogicalDisk.AllSize | Measure-Object -Sum + $IOps = Get-CimInstance Win32_PerfFormattedData_PerfDisk_PhysicalDisk -ErrorAction Ignore | + Where-Object { $_.Name -eq "_Total" } | Select-Object Name,PercentDiskTime,PercentIdleTime, + PercentDiskWriteTime,PercentDiskReadTime,CurrentDiskQueueLength,DiskBytesPersec,DiskReadBytesPersec, + DiskReadsPersec,DiskTransfersPersec,DiskWriteBytesPersec,DiskWritesPersec + $VideoCard = Get-CimInstance Win32_VideoController | Select-Object @{ + Label="VideoCard"; Expression={$_.Name}}, @{Label="Display"; Expression={ + [string]$_.CurrentHorizontalResolution+"x"+[string]$_.CurrentVerticalResolution}}, + @{Label="vRAM"; Expression={($_.AdapterRAM/1Gb)}} + $VCs = $VideoCard.vRAM | Measure-Object -Sum + $NetworkAdapter = Get-CimInstance -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=$true + $NAs = $NetworkAdapter | Measure-Object + $Collection.Add([PSCustomObject]@{ + Host = $SYS.Name + Uptime = $uptime + BootTime = $BootTime.LastBootUpTime + Owner = $SYS.PrimaryOwnerName + OS = $OS.Caption + Motherboard = $BBv + Processor = $CPU[0].Name + Core = $CPU[0].Core + Thread = $CPU[0].Thread + CPU = $CPU_Use_Proc + ProcessCount = $Process_Count + ThreadsCount = $Threads_Count + HandlesCount = $Handles_Count + MemoryAll = [string]$($MEMs.Sum/1Kb)+" GB" + MemoryUse = ($MemUse/1mb).ToString("0.00 GB") + WorkingSet = $ws + PageMemory = $pm + MemorySlots = $MEMs.Count + PhysicalDiskCount = $PDs.Count + PhysicalDiskAllSize = [string]$PDs.Sum+" Gb" + LogicalDiskCount = $LDs.Count + LogicalDiskAllSize = [string]$LDs.Sum+" Gb" + DiskTotalTime = [string]$IOps.PercentDiskTime+" %" + VideoCardCount = $VCs.Count + VideoCardAllSize = [string]$VCs.Sum+" Gb" + NetworkAdapterEnableCount = $NAs.Count + }) + $Collection + } + else { + $url = "http://$ComputerName"+":$Port/api/hardware" + $EncodingCred = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("${User}:${Pass}")) + $Headers = @{"Authorization" = "Basic ${EncodingCred}"} + try { + Invoke-RestMethod -Headers $Headers -Uri $url + } + catch { + Write-Error "Error connection" + } + } +} \ No newline at end of file diff --git a/Scripts/Get-IOps.psm1 b/Scripts/Get-IOps.psm1 new file mode 100644 index 0000000..1adb6a1 --- /dev/null +++ b/Scripts/Get-IOps.psm1 @@ -0,0 +1,6 @@ +function Get-IOps { + Get-CimInstance Win32_PerfFormattedData_PerfDisk_PhysicalDisk -ErrorAction Ignore | + Where-Object { $_.Name -ne "_Total" } | Select-Object Name,PercentDiskTime,PercentIdleTime, + PercentDiskWriteTime,PercentDiskReadTime,CurrentDiskQueueLength,DiskBytesPersec,DiskReadBytesPersec, + DiskReadsPersec,DiskTransfersPersec,DiskWriteBytesPersec,DiskWritesPersec +} \ No newline at end of file diff --git a/Scripts/Get-LD.psm1 b/Scripts/Get-LD.psm1 new file mode 100644 index 0000000..a8e4588 --- /dev/null +++ b/Scripts/Get-LD.psm1 @@ -0,0 +1,17 @@ +function Get-LD { + $LogicalDisk = Get-CimInstance Win32_logicalDisk | Where-Object {$null -ne $_.Size} | Select-Object @{ + Label="Value"; Expression={$_.DeviceID}}, @{Label="AllSize"; Expression={ + ([int]($_.Size/1Gb))}},@{Label="FreeSize"; Expression={ + ([int]($_.FreeSpace/1Gb))}}, @{Label="Free%"; Expression={ + [string]([int]($_.FreeSpace/$_.Size*100))+" %"}} + $CollectionLD = New-Object System.Collections.Generic.List[System.Object] + $LogicalDisk | ForEach-Object { + $CollectionLD.Add([PSCustomObject]@{ + Logical_Disk = $_.Value + AllSize = [string]$_.AllSize+" Gb" + FreeSize = [string]$_.FreeSize+" Gb" + Free = $_."Free%" + }) + } + $CollectionLD +} \ No newline at end of file diff --git a/Scripts/Get-MemorySize.psm1 b/Scripts/Get-MemorySize.psm1 new file mode 100644 index 0000000..f065bb8 --- /dev/null +++ b/Scripts/Get-MemorySize.psm1 @@ -0,0 +1,15 @@ +function Get-MemorySize { + $Memory = Get-CimInstance Win32_OperatingSystem + $MemUse = $Memory.TotalVisibleMemorySize - $Memory.FreePhysicalMemory + $GetProcess = Get-Process + $ws = ((($GetProcess).WorkingSet | Measure-Object -Sum).Sum/1gb).ToString("0.00 GB") + $pm = ((($GetProcess).PM | Measure-Object -Sum).Sum/1gb).ToString("0.00 GB") + $CollectionMemory = New-Object System.Collections.Generic.List[System.Object] + $CollectionMemory.Add([PSCustomObject]@{ + MemoryAll = ($memory.TotalVisibleMemorySize/1mb).ToString("0.00 GB") + MemoryUse = ($MemUse/1mb).ToString("0.00 GB") + WorkingSet = $ws + PageMemory = $pm + }) + $CollectionMemory +} \ No newline at end of file diff --git a/Scripts/Get-MemorySlots.psm1 b/Scripts/Get-MemorySlots.psm1 new file mode 100644 index 0000000..435d195 --- /dev/null +++ b/Scripts/Get-MemorySlots.psm1 @@ -0,0 +1,16 @@ +function Get-MemorySlots { + $Memory = Get-CimInstance Win32_PhysicalMemory | Select-Object Manufacturer,PartNumber, + ConfiguredClockSpeed,@{Label="Memory"; Expression={[string]($_.Capacity/1Mb)}}, + Tag,DeviceLocator,BankLabel + $CollectionMemory = New-Object System.Collections.Generic.List[System.Object] + $Memory | ForEach-Object { + $CollectionMemory.Add([PSCustomObject]@{ + Tag = $_.Tag + Model = [String]$_.ConfiguredClockSpeed+" Mhz "+$_.Manufacturer+" "+$_.PartNumber + Size = [string]($_.Memory)+" Mb" + Device = $_.DeviceLocator + Bank = $_.BankLabel + }) + } + $CollectionMemory +} \ No newline at end of file diff --git a/Scripts/Get-NetAdapter.psm1 b/Scripts/Get-NetAdapter.psm1 new file mode 100644 index 0000000..09d4fad --- /dev/null +++ b/Scripts/Get-NetAdapter.psm1 @@ -0,0 +1,8 @@ +function Get-NetAdapter { + Get-CimInstance -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=$true | Select-Object Description, + DHCPEnabled,DHCPLeaseObtained,DHCPLeaseExpires,DHCPServer, + @{Label="IPAddress"; Expression={[string]($_.IPAddress)}}, + @{Label="DefaultIPGateway"; Expression={[string]($_.DefaultIPGateway)}}, + @{Label="IPSubnet"; Expression={[string]($_.IPSubnet)}}, + MACAddress +} \ No newline at end of file diff --git a/Scripts/Get-PD.psm1 b/Scripts/Get-PD.psm1 new file mode 100644 index 0000000..7365fad --- /dev/null +++ b/Scripts/Get-PD.psm1 @@ -0,0 +1,14 @@ +function Get-PD { + $PhysicalDisk = Get-CimInstance Win32_DiskDrive | Select-Object Model, + @{Label="Size"; Expression={[int]($_.Size/1Gb)}},Partitions,InterfaceType + $CollectionPD = New-Object System.Collections.Generic.List[System.Object] + $PhysicalDisk | ForEach-Object { + $CollectionPD.Add([PSCustomObject]@{ + Model = $_.Model + Size = [string]$_.Size+" Gb" + PartitionCount = $_.Partitions + Interface = $_.InterfaceType + }) + } + $CollectionPD +} \ No newline at end of file diff --git a/Scripts/Get-Performance.psm1 b/Scripts/Get-Performance.psm1 new file mode 100644 index 0000000..72bb46a --- /dev/null +++ b/Scripts/Get-Performance.psm1 @@ -0,0 +1,12 @@ +function Get-Performance { + $GC = Get-Counter + $CollectionPerf = New-Object System.Collections.Generic.List[System.Object] + $CollectionPerf.Add([PSCustomObject]@{ + CPUTotalTime = [string]([int]($GC.CounterSamples[4].CookedValue))+" %" + MemoryUse = [string]([int]($GC.CounterSamples[2].CookedValue))+" %" + DiskTotalTime = [string]([int]($GC.CounterSamples[1].CookedValue))+" %" + AdapterName = $GC.CounterSamples[0].InstanceName + AdapterSpeed = ($GC.CounterSamples[0].CookedValue/1024/1024).ToString("0.000 MByte/Sec") + }) + $CollectionPerf +} \ No newline at end of file diff --git a/Scripts/Get-ProcessDescription.psm1 b/Scripts/Get-ProcessDescription.psm1 new file mode 100644 index 0000000..3931a43 --- /dev/null +++ b/Scripts/Get-ProcessDescription.psm1 @@ -0,0 +1,29 @@ +function Get-ProcessDescription { + param ( + $ProcessName + ) + if ($null -eq $ProcessName) { + $GetProcess = Get-Process -ErrorAction Ignore + } + else { + $GetProcess = Get-Process -Name $ProcessName -ErrorAction Ignore + } + if ($null -ne $GetProcess) { + $GetProcess | Sort-Object -Descending CPU | Select-Object ProcessName, + @{Name="TotalProcTime"; Expression={$_.TotalProcessorTime -replace "\.\d+$"}}, + @{Name="UserProcTime"; Expression={$_.UserProcessorTime -replace "\.\d+$"}}, + @{Name="PrivilegedProcTime"; Expression={$_.PrivilegedProcessorTime -replace "\.\d+$"}}, + @{Name="WorkingSet"; Expression={[string]([int]($_.WS / 1024kb))+" MB"}}, + @{Name="PeakWorkingSet"; Expression={[string]([int]($_.PeakWorkingSet / 1024kb))+" MB"}}, + @{Name="PageMemory"; Expression={[string]([int]($_.PM / 1024kb))+" MB"}}, + @{Name="VirtualMemory"; Expression={[string]([int]($_.VM / 1024kb))+" MB"}}, + @{Name="PrivateMemory"; Expression={[string]([int]($_.PrivateMemorySize / 1024kb))+" MB"}}, + @{Name="RunTime"; Expression={((Get-Date) - $_.StartTime) -replace "\.\d+$"}}, + @{Name="Threads"; Expression={$_.Threads.Count}}, + Handles,Path + } +} + +# Get-ProcessDescription * +# Get-ProcessDescription *torrent* +# Get-ProcessDescription qbittorrent \ No newline at end of file diff --git a/Scripts/Get-VideoCard.psm1 b/Scripts/Get-VideoCard.psm1 new file mode 100644 index 0000000..b92a772 --- /dev/null +++ b/Scripts/Get-VideoCard.psm1 @@ -0,0 +1,15 @@ +function Get-VideoCard { + $VideoCard = Get-CimInstance Win32_VideoController | Select-Object @{ + Label="VideoCard"; Expression={$_.Name}}, @{Label="Display"; Expression={ + [string]$_.CurrentHorizontalResolution+"x"+[string]$_.CurrentVerticalResolution}}, + @{Label="vRAM"; Expression={($_.AdapterRAM/1Gb)}} + $CollectionVC = New-Object System.Collections.Generic.List[System.Object] + $VideoCard | ForEach-Object { + $CollectionVC.Add([PSCustomObject]@{ + Model = $_.VideoCard + Display = $_.Display + VideoRAM = [string]$_.vRAM+" Gb" + }) + } + $CollectionVC +} \ No newline at end of file