Hello,
Scripting is not my thing, I am running the script below to obtain datastore space utilisation and it prompts for the username and password for vcenter, how can I add it into the script, then I can run as a scheduled task on my Windows server which has PowerCli installed?
## These are the properties assigned to the table via the ConvertTo-HTML cmdlet ##
$tableProperties = "<style>"
$tableProperties = $tableProperties + "TABLE{border-width: 1px;border-style: solid;border-color: black;}"
$tableProperties = $tableProperties + "TH{border-width: 1px;padding: 5px;border-style: solid;border-color: black;}"
$tableProperties = $tableProperties + "TD{text-align:center;border-width: 1px;padding: 5px;border-style: solid;border-color: black;}"
$tableProperties = $tableProperties + "</style>"
##The script automatically connects to the server if needed. The $server variable needs to
#be changed if the server name changes.
$server = "192.168.132.1"
If (!(connect-viserver $server -ErrorAction SilentlyContinue).IsConnected)
{
Write-Host "Connecting to server:" $server
connect-viserver $server -ErrorAction SilentlyContinue -ErrorVariable err
If($err)
{
Write-Host "Error in connecting to server, please check server name." -ForegroundColor Red
break
}
Write-Host " Complete" -ForegroundColor Green
}
## cmdlet to retrieve info from Datastores ##
## variable $rep stores set of information for each datastore ##
$rep = @()
Get-Datastore | Sort-Object -Property Name | ForEach-Object {
$Datastore = $_
$Report = "" | Select-Object -Property Datastore,CapacityGB,FreeSpaceGB,PercFreeSpace
$Report.Datastore = $Datastore.Name
$Report.CapacityGB = [Math]::Round($_.CapacityMB/1KB,0)
$Report.FreeSpaceGB = [Math]::Round($_.FreeSpaceMB/1KB,0)
$Report.PercFreeSpace = [math]::Round(((100 * ($_.FreeSpaceMB)) / ($_.CapacityMB)),0)
$rep += $Report
}
## Formats date, filename, etc ##
#Write-Host "Generating report"
$date = get-date
$datefile = get-date -uformat '%d-%m-%Y-%H%M%S'
$filename = "C:\vmware\powercli\reports\VMwareDatastoreUsage_" + $datefile + ".htm"
## Generates vsphere datastore usage report ##
Write-Host " Complete" -ForegroundColor Green
Write-Host "Generating datastore usage report"
$rep | Sort PercFreeSpace | ConvertTo-HTML -head $tableProperties -body "<th><font style = `"color:#000000`"><big><b>VMware Datastore Usage</b></big></font></th>" | Out-File $filename
Write-Host " Complete" -ForegroundColor Green
Write-Host "Your datastore usage report has been saved to:" $filename
##Send email with generated report attached##
#Write-Host "Sending email with report:" $filename + "attached"
Send-MailMessage -from "vmware-reports <vmware-reports@domain.com>" -subject "vSphere datastore usage" -to "Andy White <user1@domain.com>" -body "VMware report attached: Datastore Usage" -Attachment $filename -priority High -dno onSuccess, onFailure -smtpServer 192.168.13.3
The connection is done in this line
Connect-VIServer $server -ErrorAction SilentlyContinue -ErrorVariable err
You can add the User and Password parameters on the Connect-ViServer cmdlet. But do you want to have this info hard-coded in the script ?
An alternative is to use the Credential Store.
There are other alternatives to store credentials.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The connection is done in this line
Connect-VIServer $server -ErrorAction SilentlyContinue -ErrorVariable err
You can add the User and Password parameters on the Connect-ViServer cmdlet. But do you want to have this info hard-coded in the script ?
An alternative is to use the Credential Store.
There are other alternatives to store credentials.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
If you run the scheduled task as a user that has access to vCenter it shouldn't prompt for the password.
You could use the following when connecting to the vCenter Server
Connect-VIServer $Server -User username -Password userpassword
Where username is the name of the user you want to connect to vCenter as and userpassword is the users password, but then you will end up with the username and password in the script file in plain text.
So you could try this instead to save the password in an encrypted form
$Credential = Get-Credential username
$Credential.Password | ConvertFrom-SecureString | Set-Content credential
This will prompt you for the password for username and save it in a file named credential, you should enter the username you want to use, instead of username and you could use a filename other than credential
Then in your script use the following
$Password = Get-Content credential | ConvertTo-SecureString
$Credentail = New-Object System.Management.Automation.PSCredential(username,$Password)
Connect-VIServer $Server -Credential $Credential
The credentials store option was perfect thanks.