Author Posts

January 6, 2017 at 8:57 pm

I have three main questions.
1. System.Secuirty.SecureString I'm using "$pass = Read-Host "Prompt: " -AsSecureString
The goal is to be able to use $pass for the winrm switch -p:$pass It doesn't like this why?

2. I have a Command handler function Run-Command() which takes $command as an arg. $command is a hash table that relates to the selected method a user wants to run based on the menu's. When function is called within the script the value of $command wrote out before and its listed as System.Collections.Hashtable after the script is run you can check that variable and run the command manually using the var and it works. What am I missing?

##############################

[*] 1:GetPowerManagementCapabilities       --> Press 1 for this Option
[*] Q: QUIT                                --> Press Q to Quite
Please make a selection: 1
This value of command System.Collections.Hashtable
winrm : Error: Unknown operation name: ''
At line:1 char:1
+ winrm   -u:root -p:password -r:https://10.0.147.2/wsman -auth:basic -en ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Error: Unknown operation name: '':String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
 

PS C:\WINDOWS\system32> $command

Name                           Value                                                                                                                                     
----                           -----                                                                                                                                     
Command                        enumerate                                                                                                                                 
URI                            cimv2/CIM_PowerManagementCapabilities                                                                                                     



PS C:\WINDOWS\system32> winrm $command.Command $command.URI -u:$rac_un -p: -r:https://$rac_ip/wsman -auth:basic -encoding:utf-8 -SkipCNCheck -SkipCACheck
DCIM_CSPowerManagementCapabilities
    ElementName = Power Management Capabilities
    InstanceID = DCIM:pwrmgtcap1
    PowerChangeCapabilities = 3, 4, 7, 8
    PowerStatesSupported = 2, 5, 8, 10, 11, 12
    RequestedPowerStatesSupported = 2, 5, 8, 10, 11, 12

3. I putthis together yesterday just playing to see if I could get something that would work. Which led to the menu interfaces and switch statements. Is there a better design flow that I could use for a simple project like this one please let me know.
Eventually once all of the querying is done I will look to add methods that make config changes or schedule jobs.


$PS_METHODS = @{
    SYSTEM_METHODS = @{
        GetSystemView = @{
            Command = 'enumerate';
            URI = "cimv2/root/dcim/DCIM_SystemView"
        }
        GetVideoView = @{
            Command = 'enumerate';
            URI = 'cimv2/root/dcim/DCIM_VideoView'
        }
        GetSystemString = @{
            Command = 'enumerate';
            URI = 'cimv2/root/dcim/DCIM_SystemString'
        }
        GetComputerSystem = @{
            Command = 'enumerate';
            URI = 'cimv2/CIM_ComputerSystem'
        }
        GetCPUView = @{
            Command = 'enumerate';
            URI = 'cimv2/root/dcim/DCIM_CPUView'
        }
        GetFanViews = @{
            Command = 'enumerate';
            URI = 'cimv2/root/dcim/DCIM_FanView'
        }
        GetMemoryView = @{
            Command = 'enumerate';
            URI = 'cimv2/root/dcim/DCIM_MemoryView'
        }
        GetPCIDeviceView = @{
            Command = 'enumerate';
            URI = 'cimv2/root/dcim/DCIM_PCIDeviceView'
        }
        GetPowerSupplyView = @{
            Command = 'enumerate';
            URI = 'cimv2/root/dcim/DCIM_PowerSupplyView'
        }
        GetSystemAttribute = @{
            Command = 'enumerate';
            URI = 'cimv2/root/dcim/DCIM_SystemAttribute'
        }
        GetSystemEnumeration = @{
            Command = 'enumerate';
            URI = 'cimv2/root/dcim/DCIM_SystemEnumeration'
        }
        GetSystemInteger = @{
            Command = 'enumerate';
            URI = 'cimv2/root/dcim/DCIM_SystemInteger'
        }
    }

    BIOS_METHODS = @{
        GetBIOSEnumeration = @{
            Command = 'enumerate';
            URI = 'cimv2/root/dcim/DCIM_BIOSEnumeration'
        }
        GetBIOSInteger = @{
            Command = 'enumerate';
            URI = 'cimv2/root/dcim/DCIM_BIOSInteger'
        }
        GetBIOSString = @{
            Command = 'enumerate';
            URI = 'cimv2/root/dcim/DCIM_BIOSString'
        }
    }

    FC_METHODS = @{
        GetFCAttribute = @{
            Command = 'enumerate';
            URI = 'cimv2/root/dcim/DCIM_FCAttribute'
        }
        GetFCEnumeration = @{
            Command = 'enumerate';
            URI = 'cimv2/root/dcim/DCIM_FCEnumeration'
        }
        GetFCCapabilities = @{
            Command = 'enumerate';
            URI = 'cimv2/root/dcim/DCIM_FCCapabilities'
        }
        GetFCInteger = @{
            Command = 'enumerate';
            URI = 'cimv2/root/dcim/DCIM_FCInteger'
        }
        GetFCstatistics = @{
            Command = 'enumerate';
            URI = 'cimv2/root/dcim/DCIM_FCStatistics'
        }
        GetFCString = @{
            Command = 'enumerate';
            URI = 'cimv2/root/dcim/DCIM_FCString'
        }
        GetFCView = @{
            Command = 'enumerate';
            URI = 'cimv2/root/dcim/DCIM_FCView'
        }
    }

    NIC_METHODS = @{
        GetNICAttributes = @{
            Command = 'enumerate';
            URI = 'cimv2/root/dcim/DCIM_NICAttribute'
        }
        GetNICEnumeration = @{
            Command = 'enumerate';
            URI = 'cimv2/root/dcim/DCIM_NICEnumeration'
        }
        GetNICCapabilities = @{
            Command = 'enumerate';
            URI = 'cimv2/root/dcim/DCIM_NICCapabilities'
        }
        GetNICInteger = @{
            Command = 'enumerate';
            URI = 'cimv2/root/dcim/DCIM_NICInteger'
        }
        GetNICStatistics = @{
            Command = 'enumerate';
            URI = 'cimv2/root/dcim/DCIM_NICStatistics'
        }
        GetNICString = @{
            Command = 'enumerate';
            URI = 'cimv2/root/dcim/DCIM_NICString'
        }
        GETNICView = @{
            Command = 'enumerate';
            URI = 'cimv2/root/dcim/DCIM_NICView'
        }
    }

    RAID_METHODS = @{
        GetControllerView = @{
            Command = 'enumerate';
            URI = 'cimv2/root/dcim/DCIM_ControllerView'
        }
        GetEnclosureView = @{
            Command = 'enumerate';
            URI = 'cimv2/root/dcim/DCIM_EnclosureView'
        }
        GetPhysicalDiskView = @{
            Command = 'enumerate';
            URI = 'cimv2/root/dcim/DCIM_PhysicalDiskView'
        }
        GetRAIDEnumeration = @{
            Command = 'enumerate';
            URI = 'cimv2/root/dcim/DCIM_RAIDEnumeration'
        }
        GetRAIDInteger = @{
            Command = 'enumerate';
            URI = 'cimv2/root/dcim/DCIM_RAIDInteger'
        }
        GetRAIDString = @{
            Command = 'enumerate';
            URI = 'cimv2/root/dcim/DCIM_RAIDString'
        }
        GetVirtualDiskView = @{
            Command = 'enumerate';
            URI = 'cimv2/root/dcim/DCIM_VirtualDiskView'
        }
    }

    iDRAC_METHODS = @{
        GetiDRACCardAttributes = @{
            Command = 'enumerate';
            URI = 'cimv2/root/dcim/DCIM_iDRACCardAttribute'
        }
        GetiDRACCardEnumeration = @{
            Command = 'enumerate';
            URI = 'cimv2/root/dcim/DCIM_iDRACCardEnumeration'
        }
        GetiDRACCardInteger = @{
            Command = 'enumerate';
            URI = 'cimv2/root/dcim/DCIM_iDRACCardInteger'
        }
        GetiDRACCardString = @{
            Command = 'enumerate';
            URI = 'cimv2/root/dcim/DCIM_iDRACCardString'
        }
        GetiDRACCardView = @{
            Command = 'enumerate';
            URI = 'cimv2/root/dcim/DCIM_iDRACCardView'
        }
    }

    POWER_METHODS = @{
        GetPowerManagementCapabilities = @{
            Command = 'enumerate';
            URI = 'cimv2/CIM_PowerManagementCapabilities'
        }
    }
}

Function globals {
    # Init Vars 
    $global:rac_un = Read-Host "[*] Enter iDRAC username: "
    $global:rac_pwd = Read-Host "Enter iDRAC Password: " -AsSecureString
    $global:rac_ip = Read-Host "[*] Enter iDRAC IP Address: " 
    $global:method = $PS_METHODS
}


Function Top-Menu {
    Param(
        [string]$Title = ''
    )

    cls
    Write-Host "###############$Title###############`n" -ForegroundColor Cyan
    Write-Host "[*] 1:SYSTEM METHODS --> Press 1 for this Option" -ForegroundColor Cyan
    Write-Host "[*] 2:BIOS METHODS   --> Press 2 for this Option" -ForegroundColor Cyan
    Write-Host "[*] 3:FC METHODS     --> Press 3 for this Option" -ForegroundColor Cyan
    Write-Host "[*] 4:NIC METHODS    --> Press 4 for this Option" -ForegroundColor Cyan
    Write-Host "[*] 5:RAID METHODS   --> Press 5 for this Option" -ForegroundColor Cyan
    Write-Host "[*] 6:iDRAC METHODS  --> Press 6 for this Option" -ForegroundColor Cyan
    Write-Host "[*] 7:POWER METHODS  --> Press 7 for this Option" -ForegroundColor Cyan
    Write-Host "[*] Q: QUIT          --> Press Q to Quite" -ForegroundColor Cyan
}


Function Power-Menu {
    Param (
        [string]$power_title = ''
    )

    cls
    Write-Host "###############$drac_title###############`n" -ForegroundColor Cyan
    Write-Host "[*] 1:GetPowerManagementCapabilities       --> Press 1 for this Option" -ForegroundColor Cyan
    Write-Host "[*] Q: QUIT                                --> Press Q to Quite" -ForegroundColor Cyan
}

Function Run-Command(){
    Param (
    [parameter(Mandatory=$true, ValueFromPipeline=$true)]
    [ValidateNotNullOrEmpty()]
    [String[]]
    $command
    )
    Write-Host "This value of command $command"
    Invoke-Expression "winrm $($command.command) $($command.URI) -u:$rac_un -p:calvin -r:https://$rac_ip/wsman -auth:basic -encoding:utf-8 -SkipCNCheck -SkipCACheck"
    
}


Function Main {
    Intro
    globals

    do {
        Top-Menu
        $input = Read-Host "Please make a selection"
        switch ($input) {
            1 {System-Menu}
            2 {Bios-Menu}
            3 {FC-Menu}
            4 {NIC-Menu}
            5 {RAID-Menu}
            6 {iDRAC-Menu}
            7 {
                do {
                    Power-Menu
                    $selection = Read-Host "Please make a selection"
                    switch ($selection) {
                        1 {$global:command = $($method.POWER_METHODS.GetPowerManagementCapabilities); Run-Command($command)}
                    }
                }
                until($selection -eq 'q' -or $selection -ne $null)
            }
        }
    }
    until ($input -eq 'q' -or $input -ne $null)
}
Main

January 7, 2017 at 2:03 am

I fixed problem number 2 it was a simple mistake on my part and right in the error message of my first code block above.

However I still have an issue with using -AsSecureString for passing the password through.

Snippet where I manually put in the password in the .ps1 file

PS C:\my\Tools\Scripts> .\test.ps1

##############################
[*] 1:SYSTEM METHODS --> Press 1 for this Option
[*] 2:BIOS METHODS   --> Press 2 for this Option
[*] 3:FC METHODS     --> Press 3 for this Option
[*] 4:NIC METHODS    --> Press 4 for this Option
[*] 5:RAID METHODS   --> Press 5 for this Option
[*] 6:iDRAC METHODS  --> Press 6 for this Option
[*] 7:POWER METHODS  --> Press 7 for this Option
[*] Q: QUIT          --> Press Q to Quite
##############################
[*] 1:GetPowerManagementCapabilities       --> Press 1 for this Option
[*] Q: QUIT                                --> Press Q to Quite
DCIM_CSPowerManagementCapabilities
    ElementName = Power Management Capabilities
    InstanceID = DCIM:pwrmgtcap1
    PowerChangeCapabilities = 3, 4, 7, 8
    PowerStatesSupported = 2, 5, 8, 10, 11, 12
    RequestedPowerStatesSupported = 2, 5, 8, 10, 11, 12
PS C:\my\Tools\Scripts> Stop-Transcript
**********************
Windows PowerShell transcript end
End time: 20170106195719
**********************

Snippet of where I try and use -AsSecureString

PS C:\my\Tools\Scripts> .\test.ps1

##############################
[*] 1:SYSTEM METHODS --> Press 1 for this Option
[*] 2:BIOS METHODS   --> Press 2 for this Option
[*] 3:FC METHODS     --> Press 3 for this Option
[*] 4:NIC METHODS    --> Press 4 for this Option
[*] 5:RAID METHODS   --> Press 5 for this Option
[*] 6:iDRAC METHODS  --> Press 6 for this Option
[*] 7:POWER METHODS  --> Press 7 for this Option
[*] Q: QUIT          --> Press Q to Quite
##############################
[*] 1:GetPowerManagementCapabilities       --> Press 1 for this Option
[*] Q: QUIT                                --> Press Q to Quite
This value of command System.Collections.Hashtable
WSManFault
    Message = Access is denied.

Error number:  -2147024891 0x80070005
Access is denied.
PS C:\my\Tools\Scripts> Stop-Transcript

January 7, 2017 at 11:14 pm

Well everything is fully functional the way I wanted now.
If anyone else was looking for this

$password=read-host -assecurestring
$decodedpassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password))