# PowerShell Scripting For MOCK

#### Remote Query Hardware Information

<pre class="language-powershell"><code class="lang-powershell">// Query
<strong>Get-CimInstance -ClassName Win32_LogicalDisk -ComputerName "RemoteComputerName or IP_PC"
</strong>
Specify Parameters piping to Select-Object to identify more objects

Get-CimInstance -ClassName Win32_LogicalDisk -ComputerName "RemoteComputerName or IP_PC" | Select-Object DeviceID, DriveType, Size, FreeSpace


</code></pre>

#### Script to Automate the Remote Query and accepts a Computer name and prompts for a PC name if one is not provided

```powershell
function Get_info {
    param(
        [parameter(Mandatory, Position=0)]
        [string[]]$getPC=(Read-Host "Enter PC name: ")
    )

    if (-not $getPC) {
        Write-Host "Please provide a PC or IP address to search."
    } else {
        # Proceed with your search logic here
        foreach ($computerName in $getPC) {
            Get-CimInstance -ClassName Win32_LogicalDisk -ComputerName $computerName
        }
    }
}

# Call the function with one or more PC or IP addresses as arguments
Get_info -getPC LON-DC1

```

#### This script will accept a switch parameter to indicate whether alternate credentials are required.

```powershell
<#.
.SYNOPSIS
This function is to assist the user to query a remote computer disk size

.DESCRIPTION
Use this function to obtain the Disk Size of a computer:

Example:
To query remote PC:
Get_info -getPC "PC Name" 

To query with a remote credential:
Get_info -getPC "PC Name" -UseAlternateCredentials

.PARAMTER 

getPC - this function is the first arguement that takes a PC_name or IP as a param

AlternateCredentials - This arguement will make the function request for a user credential


#>
function Get_info {
    param(
        [parameter(Mandatory, Position=0)]
        [string[]]$getPC=(Read-Host "Enter PC name: "),

        [switch]$UseAlternateCredentials
    )
    if ($AlternateCredentials -eq $true) {
        $cred = Get-Credential
        $session = Get-CimInstance -ClassName Win32_LogicalDisk -ComputerName $computerName -Credential $cred
        Get-CimInstance -ClassName Win32_LogicalDisk -CimSession $session 
    } else {
    Get-CimInstance -ClassName Win32_LogicalDisk -ComputerName $computerName
}

# Call the function with one or more PC or IP addresses as arguments



```

#### Logging Function&#x20;

This script will accept parameters for the file name for the log file, accepts parameter for the folder containing the log file, accepts parameter for the data being written to the log file, Adds a time stamp to the data being written to the log file, & lastly appends each log entry to an existing file.

```powershell
function wLog {
    param(
        [string]$folder,
        [string]$file,
        [string]$info
    )
    
    #finding the full path
    $logP = Join-Path -Path $folder -ChildPath $file
    
    #Calc time stamp for log entry
    $date = Get-Date
    $timeStmp = $date.ToShortDateString() + " " + $date.ToLongTimeString() + ":"
    
    #Redirect info to log file
    
    $timeStmp + " " + $info | Out-File -FilePath $logP -Append
}

# Call the function with named parameters
wLog -folder "E:\Mod09" -file "testL.txt" -info "Test Log"

```

#### Creating an Error Handler on the Script

This function will create an error friendly message

```powershell
function wLog {
    param(
        [string]$folder,
        [string]$file,
        [string]$info
    )

    try {
        # Finding the full path
        $logP = Join-Path -Path $folder -ChildPath $file

        # Calculate timestamp for log entry
        $date = Get-Date
        $timeStmp = $date.ToShortDateString() + " " + $date.ToLongTimeString() + ":"

        # Redirect info to log file
        $logEntry = $timeStmp + " " + $info
        $logEntry | Out-File -FilePath $logP -Append
    }
    catch {
        # Handle any errors that occur during execution
        Write-Host "An error occurred: $($_.Exception.Message)"
    }
}

# Call the function with named parameters
wLog -folder "E:\Mod09" -file "testL.txt" -info "Test Log"

```

Reference:

<https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters?view=powershell-7.3>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://h4ck0.gitbook.io/h4ck0-blog/powershell/powershell-scripting-for-mock.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
