Global Credentials

I need to pass credentials to a function, but the function is asking me for the credential back again.

here my code

$computername = “wfcp5987i.wf.local”
$cred = Get-Credential -Message “Enter your password” -UserName “wf\ru611806”

Function Get-InfoCompSystem{
[CmdletBinding()]

param(
    [Parameter(Mandatory=$True)]
    [String]$ComputerName
  
)

Write-Verbose "Getting Server Information..." -Verbose
$cs = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $ComputerName

write-host “anotyher one”
$os = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $ComputerName
write-host “finish”

$props = @{ 'Server Name' = $ComputerName;
            'Version' = $os.version;
            'Domain'= $cs.domain;
            'RAM (GB)' = "{0:N2}" -f ($cs.TotalPhysicalMemory/1gb);
            'OS' = $os.caption;
            'Service Pack' = $os.csdversion;
            'Manufacturer' = $cs.manufacturer;
            'Model' = $cs.Model;
            'Last Reboot' = Get-LastReboot($ComputerName);
                }

New-Object -TypeName PSObject -Property $props

}

Get-InfoCompSystem -ComputerName $computername -credentials $cred

How would you pass the credential to the function

Hi Helder,

The reason you are getting asked for credentials is because they are not specified in the function you have created. They are not a param. The best way to test that params work is to tab complete them, if they don’t tab complete then they don’t work. You were almost there :slight_smile:

$computername = "wfcp5987i.wf.local"
$cred = Get-Credential -Message "Enter your password" -UserName "wf\ru611806"

Function Get-InfoCompSystem{
    [CmdletBinding()]

    param(
    [Parameter(Mandatory=$True)]
    [String]$ComputerName,

    [Parameter(Mandatory=$True)]
    [String]$credential

    )

    Write-Verbose "Getting Server Information..." -Verbose
    $cs = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $ComputerName -Credential $credential
    write-host "anotyher one"
    $os = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $ComputerName -Credential $credential
    write-host "finish"

    $props = @{ 'Server Name'  = $ComputerName;
                'Version'      = $os.version;
                'Domain'       = $cs.domain;
                'RAM (GB)'     = "{0:N2}" -f ($cs.TotalPhysicalMemory/1gb);
                'OS'           = $os.caption;
                'Service Pack' = $os.csdversion;
                'Manufacturer' = $cs.manufacturer;
                'Model'        = $cs.Model;
                'Last Reboot'  = ($os | select @{LABEL='LastBootUpTime' ;EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)}}).LastBootUpTime
    }

    New-Object -TypeName PSObject -Property $props

}
Get-InfoCompSystem -ComputerName $computername -credential $cred

Well, you do have some things that I’d consider a bit off in the script.
First…is this set of creds…
$cred = Get-Credential -Message “Enter your password” -UserName “wf\ru611806”

… an admin on this host…
$computername = “wfcp5987i.wf.local”
… if not you are going to get prompted for a set of creds whish are on the remote host and is an admin.
Secondly, .local is really not a thing to use in AD these days. Even in test labs. See MS TechNet on the topic.

The prompt thing is also caused by where and how you are asking for it

Yet, if you are just after basic system inventory, why are you hand writing all this (well, other than a learning task)?

Depending on want version of the OS you are running as well as the version of PoSH you are on. You can get Computer Info using the Get-ComputerInfo cmdlet or even just the built-in SystemInfo command.

There are also many pre-built computer inventory scripts / modules on the MS TechNet, MSDN , and PowerShellGallery sites.

Here are just a few examples, see if they will get you what you need.

Powershell Computer or Server Hardware Inventory
gallery.technet.microsoft.com/Powershell-Computer-or-6cb1f0f0

Asset Inventory 1.0.1.6
Collects asset information including software inventory, windows updates, and system information.
PowerShell Gallery | AssetInventory 1.0.1.6

Inventorying Computers with AD PowerShell
blogs.technet.microsoft.com/askds/2010/02/04/inventorying-computers-with-ad-powershell

Let PowerShell do an Inventory of your Servers
Let PowerShell do an Inventory of your Servers - Simple Talk