Base - Add the user name and password in script?

Hello

Scripting is not my thing, I run the script below in order to get the use of the data store space and it requires user name and password for vcenter, how do I add it in the script, then I can run as a scheduled task on my Windows Server that has installed PowerCli?

# 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 will automatically connect to the server if necessary. The $server variable must

#be changed if the server name changes.

$server = "192.168.132.1".

If (!) () to connect-viserver $server - ErrorAction SilentlyContinue). IsConnected)

{

Write-Host "connection to the server: ' $server

to connect-viserver $server - ErrorAction SilentlyContinue err - ErrorVariable

If ($Err)

{

Write-Host "Connection to the server error, please check server name." - ForegroundColor Red

breaking

}

Write-Host "Full" - ForegroundColor Green

}

# cmdlet to retrieve information from data warehouses.

# variable $rep stores all the information for each data store.

$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]: tour ($_.) CapacityMB / 1 KB, 0)

$Report.FreeSpaceGB = [math]: tour ($_.) FreeSpaceMB / 1 KB, 0)

$Report.PercFreeSpace = [math]: Round (((100 * ($_.))) FreeSpaceMB)) / ($_.) (CapacityMB)), 0)

$rep += $Report

}

# Formats date, file name, etc. #.

#Write - Host "Generating report".

$date = get-date

$datefile = get-date - uformat ' %d-%m - Y % - hour %M ' %S

$filename = "C:\vmware\powercli\reports\VMwareDatastoreUsage_" + $datefile + ".htm".

# Generates vsphere datastore usage report #.

Write-Host "Full" - 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 using < /b > < / big > < / font > < /th > | Out-file $filename

Write-Host "Full" - ForegroundColor Green

Write-Host "usage of your data store report has been saved to: ' $filename

##Send email with attached generated report #.

#Write - Host "sending email with report:" $filename + "attached".

Send-MailMessage-to "vmware-reports < . " [email protected] >"-subject 'vSphere datastore use' - to ' Andy White < [email protected] > "-body" VMware report attached: use of the data store "- attachment $filename - high - dno onSuccess, onFailure - smtpServer 192.168.13.3

The connection is made in this line

SE connect-VIServer $server - ErrorAction SilentlyContinue err - ErrorVariable

You can add the user and password parameters of the cmdlet Connect-ViServer . But you want to have this info hardcoded in the script?

An alternative is to use the credentials store.

There are other alternatives to store the credentials.

Tags: VMware

Similar Questions

Maybe you are looking for