bulletp31
Enthusiast
Enthusiast

vdf -h Powershell equivalent ?

Jump to solution

Just asking if there is a cmdlet in powershell to give the same result as vdf -h.

Any Help Much appriciated.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

The vdf -h command is executed in the COS and there is no PowerCLI cmdlet to offer the same functionality and output.

You can use plink.exe to execute the vdf -h command in the COS and capture the output in a PowerShell variable.

$User = <user>
$Pswd = <password>
$Computer = <ESX-hostname>
$plink = <PuTTY-path>\plink.exe"
$plinkoptions = " -v -batch -pw $Pswd"

$cmd1 = '/usr/sbin/vdf -h'
$remoteCommand = '"' + $cmd1 + '"'
$command = $plink + " " + $plinkoptions + " " + $User + "@" + $computer + " " + $remoteCommand

$msg = Invoke-Expression -command $command 
$msg

Note that you need to use an account that can ssh to the COS and which has the required permissions to execute the command.

You can now handle the output that is stored in the $msg variable.

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
8 Replies
LucD
Leadership
Leadership

The vdf -h command is executed in the COS and there is no PowerCLI cmdlet to offer the same functionality and output.

You can use plink.exe to execute the vdf -h command in the COS and capture the output in a PowerShell variable.

$User = <user>
$Pswd = <password>
$Computer = <ESX-hostname>
$plink = <PuTTY-path>\plink.exe"
$plinkoptions = " -v -batch -pw $Pswd"

$cmd1 = '/usr/sbin/vdf -h'
$remoteCommand = '"' + $cmd1 + '"'
$command = $plink + " " + $plinkoptions + " " + $User + "@" + $computer + " " + $remoteCommand

$msg = Invoke-Expression -command $command 
$msg

Note that you need to use an account that can ssh to the COS and which has the required permissions to execute the command.

You can now handle the output that is stored in the $msg variable.

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
bulletp31
Enthusiast
Enthusiast

Yea i know the command is only a OS command but was just wondering if there was a way of pulling from the OS using powershell. But thinking about it the vCenter dosnt even do that.

I will leace it for now, but thanks for the help.

0 Kudos
r_ffin
Contributor
Contributor

Worth mentioning that if you have $ErrorActionPreference = "Stop"; somewhere, you'll likely bust out on the Invoke-Expression line almost immediately with an error containing Looking up host "10.10.10.1", (with the address you're using, of course), and you'll never get a value returned in $msg.

I'm guessing plink sends all of the -verbose text back along what PowerShell sees as an error stream, and PowerShell sees that first bit of feedback as an error -- and stops!

Easy enough to fix, afaict; just remove the -v verbose flag from the call.

$plinkoptions = " -v -batch -pw $Pswd"

... to...
$plinkoptions = " -batch -pw $Pswd"

Step 3.) Profit.

Neat trick. Thanks!

0 Kudos
Jasper552
Contributor
Contributor

Thank you for this script, saved me a lot of time.

I did run into several problems while attempting to use this script in my environment. I am running Powershell v2.0, plink.exe v0.68.0.0, and connecting to an Esxi 5.5.0 server.

--------------------------

The first issue I ran into involved DNS, and while troubleshooting with "Invoke-Expression -Command Test-Connection $computer" gave me an error along the lines of:

"failed: The requested name is valid, but no data of the requested type was found"

After A LOT of tests and failures, I discovered that converting the fully qualified domain name of the host to upper case before being passed to the command solved the problem.

>>$Computer = <ESX-hostname>

Note:This line was changed to a user-prompt for our use-case.

The working result is:

$Computer = Read-Host -Prompt 'Hostname:'

$Computer.ToUpper()

--------------------------

The next issue I came across involved the password string passed to the command. The errors I was getting was:

"Attempting keyboard-interactive authentication

Keyboard-interactive authentication failed

Access denied

Attempting keyboard-interactive authentication

Disconnected: Unable to authenticate"

This turned out to be caused by the way PowerShell 2.0 interprets string variables when they are enclosed within a quoted string.

>> $plinkoptions = " -v -batch -pw $Pswd" <<

This line needed to be changed to:

$plinkoptions = " -v -batch -pw $($Pswd)"

--------------------------

The resulting code is attached.

Hope this helps if anyone is running into these errors.

0 Kudos
LucD
Leadership
Leadership

Thanks for sharing!

PS: you should perhaps consider upgrading your PowerShell version


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Jasper552
Contributor
Contributor

Wow, nice catch. I didn't realize it was so far behind. Thanks.

0 Kudos
tdubb123
Expert
Expert

any idea how can i run this with powershell on osX?

plink.exe is not a format osX can recognize

0 Kudos
LucD
Leadership
Leadership

I would suggest to use the Posh-Ssh module, see my Use Posh-SSH instead of PuTTY dive.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos