VMware Horizon Community
MattDav
Enthusiast
Enthusiast
Jump to solution

Update Datastores for a VMware Pool using PowerCli

Using this as a starting block.

VMware View 5.2 Documentation Library

I want to combine two functions, and have the variables use ".txt" files with my new and old datastores listed.

I've edited it a bit, combining the two functions, and creating variables for new and old lists, but I'm not sure on the context to provide to the variables for the text files.  Any PowerShell / PowerCli guru's?

# A PowerShell function to add new, then remove old datastores from an automatic pool.

# UpdateDatastoresForAutomaticPool

# Parameters

#   $Pool          Pool ID of pool to be updated.

#   $OldDatastore     Full path to OldDatastore.txt to be removed.

#   $NewDatastore     Full path to NewDatastore.txt to be added.

$Pool = "C:\powercli\PersistentPools.txt"

$OldDatastore = "C:\powercli\OldDatastore.txt"

$NewDatastore = "C:\powercli\NewDatastore.txt"

function RemoveDatastoreFromAutomaticPool

{ param ($Pool, $OldDatastore)

    $PoolSettings = (Get-Pool -pool_id $Pool)

    $currentdatastores = $PoolSettings.datastorePaths

    $datastores = ""

    foreach ($path in $currentdatastores.split(";")){

        if(-not ($path -eq $OldDatastore)){

            $datastores = $datastores + "$path;"

        }

    }

    Update-AutomaticPool -pool_id $Pool -datastorePaths $datastores

}

function AddDatastoreToAutomaticPool

{ param ($Pool, $NewDatastore)

    $PoolSettings = (Get-Pool -pool_id $Pool)

    $datastores = $PoolSettings.datastorePaths + ";$NewDatastore"

    Update-AutomaticPool -pool_id $Pool -datastorePaths $datastores

}

Thanks,

-Matt

Reply
0 Kudos
1 Solution

Accepted Solutions
mpryor
Commander
Commander
Jump to solution

You are using literal strings rather than content for files. Assuming the file contents is a list with one entry on each line, you must modify your code to actually look at the data within, e.g:

$oldstores = get-content  "C:\powercli\OldDatastore.txt"

foreach ($path in $currentdatastores.split(";")) {

    if (-not ($oldstores -contains $path)) {

        ...

    }

}

View solution in original post

Reply
0 Kudos
2 Replies
mpryor
Commander
Commander
Jump to solution

You are using literal strings rather than content for files. Assuming the file contents is a list with one entry on each line, you must modify your code to actually look at the data within, e.g:

$oldstores = get-content  "C:\powercli\OldDatastore.txt"

foreach ($path in $currentdatastores.split(";")) {

    if (-not ($oldstores -contains $path)) {

        ...

    }

}

Reply
0 Kudos
MattDav
Enthusiast
Enthusiast
Jump to solution

Awesome, thanks!

Reply
0 Kudos