Hello folks, is there a way to create a function to collect Hostname where script runs?
Like a computer Name, User who initiated the script?
What sort of script?
Where is the script running? (on your vCenter Server, on your ESXi hosts, or in your VM guest OSes)
The script will run from a vm to connect to vcenter ☺️☺️
And what will it be asking vCenter to do?
the script will be launched from a virtual machine, to connect to vcenter, I would need to collect the information, the user who launched the script and from which machine (IP-FQDN)
If you want to get the user and host name of where said script is ran this doesn't sound like anything vmware related, just use PowerShell standard commands
PowerTip: Use PowerShell to Get Computer Name | Scripting Blog
https://stackoverflow.com/questions/7505792/powershell-find-the-user-who-invoked-the-script
I've created the below script, unfortunately not able to find away to get the user logged in
Function ConnectTo-vCenter
{
$connection = Get-Credential -Message “Please enter your password”
foreach ($vCenter in $vCenterList){
Fonc_LOG "Connecting to $vCenter..."
Connect-VIServer -Server $vCenter -Credential $connection
If($? -Eq $True){
Fonc_LOG "Successfully connected to: $vCenter as $($vCenter.User)."
}
else {
Fonc_LOG "Not Connected to $vCenter"
}
}
}
ConnectTo-vCenter
$test=Connect-VIServer -Server $vCenter -Credential $connection
$test.user --> the name of the user logged in
Moderator: Thread moved to the PowerCLI area.
unfortunately it's not working
then show me your code - in my environment its working
Function ConnectTo-vCenter
{
$connection = Get-Credential -Message “Please enter your Login & password”
$viServer = Connect-VIServer Server $vCenter -Credential $connection -Port 443
foreach ($vCenter in $vCenterList)
{
Fonc_LOG "Connecting to $vCenter..."
if ($viServer.IsConnected) {
Fonc_LOG "Successfully connected to $vCenter as $viServer"
}
else {
Fonc_LOG "Not Connected to $vCenter Please Check your Credential)"
}
}
}
ConnectTo-vCenter