Here is what I tried based on the other script and the errors I am getting.
$rootUser = 'root'
$rootPswd = '*****'
$userName = 'SOLSNMP'
$esxName = 'pcoesxvm02.sgsolutions.local'
$esx = Get-VMHost -Name $esxName
Connect-VIServer -Server $esx.Name -User $rootUser -Password $rootPswd > $null
Try{
$user = Get-VMHostAccount -User $userName -Server $esx.Name -ErrorAction Stop
}
Catch{
$user = New-VMHostAccount -Id $userName -Password $pswd -GrantShellAccess -Server $esx.Name
}
$perm = Get-VIPermission -Principal $userName -Server $esx.Name
if(!$perm){
$root = Get-Folder -Name root -Server $esx.Name
New-VIPermission -Entity $root -Principal $userName -Role Read-only -Server $esx.Name
}
>>
New-VMHostAccount : Cannot bind argument to parameter 'Password' because it is
null.
At line:5 char:55
+ $user = New-VMHostAccount -Id $userName -Password $pswd
-GrantShellAccess -S ...
+ ~~~~~
+ CategoryInfo : InvalidData: (:) [New-VMHostAccount], ParameterB
indingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,V
Mware.VimAutomation.ViCore.Cmdlets.Commands.Host.NewVMHostAccount
PowerCLI C:\> $perm = Get-VIPermission -Principal $userName -Server $esx.Name
Get-VIPermission : 9/19/2017 3:40:56 PM Get-VIPermission Could not
find VIAccount with name 'SOLSNMP'.
At line:1 char:9
+ $perm = Get-VIPermission -Principal $userName -Server $esx.Name
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (SOLSNMP:String) [Get-VIPermissi
on], VimException
+ FullyQualifiedErrorId : Core_ObnSelector_SelectObjectByNameCore_ObjectNo
tFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.PermissionManagement.G
etVIPermission
Get-VIPermission : 9/19/2017 3:40:56 PM Get-VIPermission VIAccount
parameter: Could not find any of the objects specified by name.
At line:1 char:9
+ $perm = Get-VIPermission -Principal $userName -Server $esx.Name
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (VMware.VimAutom...unt[] Princip
al:RuntimePropertyInfo) [Get-VIPermission], ObnRecordProcessingFailedExcep
tion
+ FullyQualifiedErrorId : Core_ObnSelector_SetNewParameterValue_ObjectNotF
oundCritical,VMware.VimAutomation.ViCore.Cmdlets.Commands.PermissionManage
ment.GetVIPermission
PowerCLI C:\> if(!$perm){
>> $root = Get-Folder -Name root -Server $esx.Name
>> New-VIPermission -Entity $root -Principal $userName -Role Read-only -Serv
er $esx.Name
>> }
>>
Get-Folder : 9/19/2017 3:40:56 PM Get-Folder Folder with name 'root'
was not found using the specified filter(s).
At line:2 char:13
+ $root = Get-Folder -Name root -Server $esx.Name
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (:) [Get-Folder], VimException
+ FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimA
utomation.ViCore.Cmdlets.Commands.GetFolder
New-VIPermission : Cannot validate argument on parameter 'Entity'. The
argument is null or empty. Supply an argument that is not null or empty and
then try the command again.
At line:3 char:30
+ New-VIPermission -Entity $root -Principal $userName -Role Read-only
-Server ...
+ ~~~~~
+ CategoryInfo : InvalidData: (:) [New-VIPermission], ParameterBi
ndingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.V