VMware Cloud Community
SCharchouf
Hot Shot
Hot Shot

Creat function to collect ComputerName & user info

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?

11 Replies
scott28tt
VMware Employee
VMware Employee

What sort of script?

Where is the script running? (on your vCenter Server, on your ESXi hosts, or in your VM guest OSes)


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

Although I am a VMware employee I contribute to VMware Communities voluntarily (ie. not in any official capacity)
VMware Training & Certification blog
0 Kudos
SCharchouf
Hot Shot
Hot Shot

The script will run from a vm to connect to vcenter ☺️☺️

0 Kudos
scott28tt
VMware Employee
VMware Employee

And what will it be asking vCenter to do?


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

Although I am a VMware employee I contribute to VMware Communities voluntarily (ie. not in any official capacity)
VMware Training & Certification blog
0 Kudos
SCharchouf
Hot Shot
Hot Shot

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)

0 Kudos
sjesse
Leadership
Leadership

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

SCharchouf
Hot Shot
Hot Shot

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

0 Kudos
berndweyand
Expert
Expert

$test=Connect-VIServer -Server $vCenter -Credential $connection

$test.user --> the name of the user logged in

scott28tt
VMware Employee
VMware Employee

Moderator: Thread moved to the PowerCLI area.


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

Although I am a VMware employee I contribute to VMware Communities voluntarily (ie. not in any official capacity)
VMware Training & Certification blog
0 Kudos
SCharchouf
Hot Shot
Hot Shot

unfortunately it's not working Smiley Sad

0 Kudos
berndweyand
Expert
Expert

then show me your code - in my environment its working

0 Kudos
SCharchouf
Hot Shot
Hot Shot

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

0 Kudos