VMware Cloud Community
ajohn24
Contributor
Contributor
Jump to solution

Automating scratch partition

Hi Luc,

I was following one of your posts on how to automate the scratch partition. But it does not seem to work for me. Attached is the code.

The script just runs, and ask to disconnect from the server but does not configure anything. It actually disconnects twice if you see the output below:

cmdlet Get-Credential at command pipeline position 1
Supply values for the following parameters:
Credential
Please enter the credentials to login to the server abc.com

IsConnected   : True
Id            : /VIServer=root@abc.com:443/
ServiceUri    : https://abc.com/sdk
SessionSecret : 524c0c91-297b-d974-aeee-a63d0add4c8c
Name          : abc.com
Port          : 443
SessionId     : 524c0c91-297b-d974-aeee-a63d0add4c8c
User          : root
Uid           : /VIServer=root@abc.com:443/
Version       : 5.1
Build         : 1483097
ProductLine   : embeddedEsx
InstanceUuid  :
RefCount      : 2
ExtensionData : VMware.Vim.ServiceInstance


Confirm
Are you sure you want to perform this action?
Performing operation "Disconnect VIServer" on Target "User: root, Server: abc.com, Port: 443".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"):
Please enter the credentials to login to the server abc.com

IsConnected   : True
Id            : /VIServer=root@abc.com:443/
ServiceUri    : https://abc.com/sdk
SessionSecret : 524c0c91-297b-d974-aeee-a63d0add4c8c
Name          : abc.com
Port          : 443
SessionId     : 524c0c91-297b-d974-aeee-a63d0add4c8c
User          : root
Uid           : /VIServer=root@abc.com:443/
Version       : 5.1
Build         : 1483097
ProductLine   : embeddedEsx
InstanceUuid  :
RefCount      : 2
ExtensionData : VMware.Vim.ServiceInstance


Confirm
Are you sure you want to perform this action?
Performing operation "Disconnect VIServer" on Target "User: root, Server: abc.com, Port: 443".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"):
Disconnecting from the server vc.com..

Please suggest.

Thx,

AJ

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

To remember the location you can do the following (I only show the relevant part).

if($ds){

    Push-Location

    Set-Location -Path $ds.DatastoreBrowserPath

    New-Item $tgtName -ItemType directory

    Pop-Location

    Set-VMHostAdvancedConfiguration -Name $configName -Value ('/vmfs/volumes/' + $ds.Name + '/' + $tgtName) -Server $esxi -Confirm:$false

Afaik, a reboot of the ESXi is required to activate the change


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
22 Replies
LucD
Leadership
Leadership
Jump to solution

Did you run this with DefaultVIServerMode set to Multiple (see the Set-PowerCLIConfiguration cmdlet) ?

If your are in single mode, it is normal that the script first disconnects from the vSphere server you are connected to.

In your case it seems you were already connected to the specific ESXi server.

Do you have a local datastore connected to the ESXi that has the string "local" in the datastorename ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
ajohn24
Contributor
Contributor
Jump to solution

Hi Luc,

This function is a subscript of another script. I did not have DefaultVIServerMode set inside the function but in the script itself. I have done the changes now though within the function too.

Yes there is a local datastore. If we run the command manually I am able to fetch the desired info

PowerCLI C:\Scripts> Get-Datastore | where {$_.Name -match "local"}

Name                               FreeSpaceMB      CapacityMB
----                                        ----------                     ----------
abc_local_datastoreI              190060          220832

But when i run the script the scratch location does not gets configured.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Did you take the different scopes into account when changing the DefaultVIServerMode ?

Do a "Get-PowerCLIConfiguration" inside the function to see what is actually in use.

Did you get any error messages ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
ajohn24
Contributor
Contributor
Jump to solution

Yes it was giving the desired output. But I have inserted the lines again. Attaching both the script and output to the post.

It does not give any error but does not even set the scratch partition.

Thx,

AJ

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try adding the Server parameter on the cmdlets.

Something like this

Function Set-Scratch

{

$mode,$warn = Get-PowerCLIConfiguration | %{$_.DefaultVIServerMode,$_.DisplayDeprecationWarnings}

Set-PowerCLIConfiguration -DefaultVIServerMode "Multiple" -DisplayDeprecationWarnings:$false

$esxhost = "abc.com"

$esxcred = Get-Credential

$configName = 'ScratchConfig.ConfiguredScratchLocation'

Get-VMHost -Name $esxhost | where {$_.ExtensionData.Config.Product.ProductLineId -eq "embeddedEsx"} | %{

     $esxi = Connect-VIServer -Server $_.Name -credential $esxcred

     $scratch = (Get-VMHostAdvancedConfiguration -VMHost $_ -Server $esxi)[$configName]

        $tgtName = ".locker-" + $_.Name.Split('.')[0]

        if($scratch -notmatch ($tgtName + "$")){

        $ds = Get-Datastore -VMHost $_ -Server $esxi | where {$_.Name -match "local"} | select -first 1

        if($ds){

            Set-Location -Path $ds.DatastoreBrowserPath

            New-Item $tgtName -ItemType directory

            Set-VMHostAdvancedConfiguration -Name $configName -Value ('/vmfs/volumes/' + $ds.Name + '/' + $tgtName) -Server $esxi -Confirm:$false

       }

    }

    Disconnect-VIServer -Server $_.Name -Confirm:$false

}

Set-PowerCLIConfiguration -DefaultVIServerMode $mode -DisplayDeprecationWarnings:$warn

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
ajohn24
Contributor
Contributor
Jump to solution

It is still the same. I added a simple write statement just after the set-vmhostadvancedconfiguration block and it does not get executed. It means somehow the code within if($ds) is not getting executed. But I am not sure why.

Attaching the script again. I am somehow unable to paste the contents in here :smileyconfused: and below is the output

Perform operation?
Performing operation 'Update vSphere PowerCLI configuration.'?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"):
UseSystemProxy  Multiple

cmdlet Get-Credential at command pipeline position 1
Supply values for the following parameters:
Credential

Perform operation?
Performing operation 'Update vSphere PowerCLI configuration.'?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"):
UseSystemProxy  Multiple
Disconnecting from the server vc.com..

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That looks as if the local datastore is not retrieved, or has a different name, when connected directly to the ESXi server.

Can you check ?

Do a Connect-VIServer to the ESXi server, and then perform a Get-Datastore.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
ajohn24
Contributor
Contributor
Jump to solution

Yes but when i run it on the host locally I get the required information

PowerCLI C:\Scripts> Connect-VIServer abc.com

Name                           Port  User
----                           ----  ----
abc.com         443   root

PowerCLI C:\Scripts> Get-Datastore | where {$_.Name -match "local"}

Name                               FreeSpaceMB      CapacityMB
----                                        ----------                     ----------
abc_local_datastoreI              190060          220832

Infact the local datastore is the only one we have on the host currently.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you add the following line, just before the line where you populate the $ds variable

Get-Datastore -VMHost $_ -Server $esxi

Just to check what is returned.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
ajohn24
Contributor
Contributor
Jump to solution

It does not return anything. The output is the same as above. Prompts for the credential and logs out of the VC

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Strange.

Which connections are open at that point ?

Can you display the contents of $Global:DefaultVIServers ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
ajohn24
Contributor
Contributor
Jump to solution

This is what i get in the output. I pasted it just after I connect to the host. Output attached.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You seem to have 2 connections to the vCenter and 2 to the ESXi server, but that in itself could not cause the problem you are seeing.

If have the impression the $ds variable is empty, and hence the script will not reach the cmdlet to set the scratch partition.

Did you already check if there is anything in the $ds variable, just before the If statement ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

ajohn24
Contributor
Contributor
Jump to solution

Thanks for all the pointers till now. Ok, so i tried testing the value of $ds variable just placing simple write-host blocks before and after $ds variable as in the script attached but the output remains to be the same.:smileyplain:

Means, it's not able to retrieve the values of $scratch or $tgtname and meet the condition in the if block. Not sure what I am doing wrong though.:smileyconfused:


Reply
0 Kudos
ajohn24
Contributor
Contributor
Jump to solution

I tested another thing. Since this function is just a part of my script and assuming I might be messing it up in some other parts I created a separate script just with this code itself but it still remains the same Smiley Sad. Output does not change
a bit:

PowerCLI C:\Scripts> .\set_scratch_test.ps1

Perform operation?
Performing operation 'Update vSphere PowerCLI configuration.'?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"):

Proxy Policy    Default Server
                Mode
------------    ---------------
UseSystemProxy  Multiple

cmdlet Get-Credential at command pipeline position 1
Supply values for the following parameters:
Credential

Perform operation?
Performing operation 'Update vSphere PowerCLI configuration.'?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"):
UseSystemProxy  Multiple

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Are you sure you are even executing the Set-VMHostAdvancedConfiguration cmdlet ?

Did you check if there is anything in the $ds variable before you execute the If statement ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
ajohn24
Contributor
Contributor
Jump to solution

I am executing the script in entirety. I did it separately as well as in the function. But surely the set-vmhostadvancedconfiguration is not getting run.

Like i said earlier I manually ran two write-host blocks inside the code but did not get any output. Reattaching the script. Seems it's not able to retrieve the valuse for $scratch and $tgtName itself.

Reply
0 Kudos
ajohn24
Contributor
Contributor
Jump to solution

Hi Luc,

Any suggestions?

Reply
0 Kudos
ajohn24
Contributor
Contributor
Jump to solution

Luc,

Still awaiting your revert on this

Reply
0 Kudos