<\/em>Hi everyone<\/p>\n
I have a script that we can use to create a list of VMs, their IPs, RAM, OS and CPUs from all of our vCenters.<\/p>\n This works pretty well, but we are moving and updating a lot of VMs over the next couple of months. I would like to be able to schedule this, but each vCenter prompts me for my login info. How can I edit this script so it would use either the current login account, or the account that is running the script to run<\/p>\n I would also like it to email me the file each week, but I have the function for that, and can put that in very easily.<\/p>\n Any help is appreciated<\/p>\n thanks y’all<\/p>\n app<\/p>","upvoteCount":6,"answerCount":8,"datePublished":"2015-06-04T12:35:15.000Z","author":{"@type":"Person","name":"apperrault","url":"https://community.spiceworks.com/u/apperrault"},"acceptedAnswer":{"@type":"Answer","text":" It looks like Connect-ViServer takes a [PSCredential] object for it’s -Credential parameter. You can save a credential to disk using Export-CliXml and use that later:<\/p>\n To save a credential to disk:<\/p>\n To import a credential object in a script:<\/p>\n Then you can call Connect-ViServer with the -Credential parameter:<\/p>\n Note that these saved credentials will only work for the user that saved them on the same computer. That means you need to create the saved credential using the user that will ultimately be used to launch the scheduled task and on the computer that will run the task.<\/p>","upvoteCount":2,"datePublished":"2015-06-04T12:51:23.000Z","url":"https://community.spiceworks.com/t/can-you-help-streamline-and-fix-my-script/409187/3","author":{"@type":"Person","name":"mattmcnabb","url":"https://community.spiceworks.com/u/mattmcnabb"}},"suggestedAnswer":[{"@type":"Answer","text":" <\/em>Hi everyone<\/p>\n I have a script that we can use to create a list of VMs, their IPs, RAM, OS and CPUs from all of our vCenters.<\/p>\n This works pretty well, but we are moving and updating a lot of VMs over the next couple of months. I would like to be able to schedule this, but each vCenter prompts me for my login info. How can I edit this script so it would use either the current login account, or the account that is running the script to run<\/p>\n I would also like it to email me the file each week, but I have the function for that, and can put that in very easily.<\/p>\n Any help is appreciated<\/p>\n thanks y’all<\/p>\n app<\/p>","upvoteCount":6,"datePublished":"2015-06-04T12:35:15.000Z","url":"https://community.spiceworks.com/t/can-you-help-streamline-and-fix-my-script/409187/1","author":{"@type":"Person","name":"apperrault","url":"https://community.spiceworks.com/u/apperrault"}},{"@type":"Answer","text":" Use Get-Credential and store your cred into a variable and pending that it is the same for all servers, you can pass $cred to each one.<\/p>\n There are some varied methods on storing these credentials - here’s one that seems to be pretty safe:<\/p>\n
connect-viserver vcentsy01ewdu\n$vms = get-view -viewtype \"VirtualMachine\" -Filter @{\"runtime.PowerState\" = \"poweredOn\"}\n$vmlist += $vms\ndisconnect-viserver vcentsy01ewdu -confirm:$false\n\nconnect-viserver vcentsy02ewdu\n$vms = get-view -viewtype \"VirtualMachine\" -Filter @{\"runtime.PowerState\" = \"poweredOn\"}\n$vmlist += $vms\ndisconnect-viserver vcentsy02ewdu -confirm:$false\n\nconnect-viserver vcentsy03ewdu\n$vms = get-view -viewtype \"VirtualMachine\" -Filter @{\"runtime.PowerState\" = \"poweredOn\"}\n$vmlist += $vms\ndisconnect-viserver vcentsy03ewdu -confirm:$false\n\nconnect-viserver vcentsy08ewdu\n$vms = get-view -viewtype \"VirtualMachine\" -Filter @{\"runtime.PowerState\" = \"poweredOn\"}\n$vmlist += $vms\ndisconnect-viserver vcentsy08ewdu -confirm:$false\n\nconnect-viserver vcentsy01ewwe\n$vms = get-view -viewtype \"VirtualMachine\" -Filter @{\"runtime.PowerState\" = \"poweredOn\"}\n$vmlist += $vms\ndisconnect-viserver vcentsy01ewwe -confirm:$false\n\n$vmlist|select-object {$_.Config.Name}, {$_.Config.GuestFullName}, {$_.config.hardware.numcpu}, {$_.config.hardware.memorymb}, {$_.guest.IPAddress}|export-csv c:\\temp\\dublinwe.csv\n<\/code><\/pre>\n
Get-Credential | Export-CliXml -Path c:\\creds\\username_computername.clixml\n<\/code><\/pre>\n
$Credential = Import-CliXml -Path c:\\creds\\username_computername.clixml\n<\/code><\/pre>\n
Connect-ViServer -Server 'servername' -Credential $Credential\n<\/code><\/pre>\n
connect-viserver vcentsy01ewdu\n$vms = get-view -viewtype \"VirtualMachine\" -Filter @{\"runtime.PowerState\" = \"poweredOn\"}\n$vmlist += $vms\ndisconnect-viserver vcentsy01ewdu -confirm:$false\n\nconnect-viserver vcentsy02ewdu\n$vms = get-view -viewtype \"VirtualMachine\" -Filter @{\"runtime.PowerState\" = \"poweredOn\"}\n$vmlist += $vms\ndisconnect-viserver vcentsy02ewdu -confirm:$false\n\nconnect-viserver vcentsy03ewdu\n$vms = get-view -viewtype \"VirtualMachine\" -Filter @{\"runtime.PowerState\" = \"poweredOn\"}\n$vmlist += $vms\ndisconnect-viserver vcentsy03ewdu -confirm:$false\n\nconnect-viserver vcentsy08ewdu\n$vms = get-view -viewtype \"VirtualMachine\" -Filter @{\"runtime.PowerState\" = \"poweredOn\"}\n$vmlist += $vms\ndisconnect-viserver vcentsy08ewdu -confirm:$false\n\nconnect-viserver vcentsy01ewwe\n$vms = get-view -viewtype \"VirtualMachine\" -Filter @{\"runtime.PowerState\" = \"poweredOn\"}\n$vmlist += $vms\ndisconnect-viserver vcentsy01ewwe -confirm:$false\n\n$vmlist|select-object {$_.Config.Name}, {$_.Config.GuestFullName}, {$_.config.hardware.numcpu}, {$_.config.hardware.memorymb}, {$_.guest.IPAddress}|export-csv c:\\temp\\dublinwe.csv\n<\/code><\/pre>\n