GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — PowerCLI-Example-Scripts/Scripts/SetDatastoreTag.ps1 at master · vmware-archive/PowerCLI-Example-Scripts

This repository was archived by the owner on Jan 20, 2026. It is now read-only.

Expand file tree

/

SetDatastoreTag.ps1

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
executable file
·
198 lines (177 loc) · 5.44 KB
/

SetDatastoreTag.ps1

Copy path

File metadata and controls

executable file
·
198 lines (177 loc) · 5.44 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<#
.SYNOPSIS
A brief description of the file.
.DESCRIPTION
Given a list of Datastore Names, this script will assign a Tag to them
.PARAMETER csvFile
String representing the full path of the file
The file must be structured like this:
-----------------------------
Tag1,Tag2,Tag3,Tag4
IPv4-iSCSI-SiteA,Tag1,Tag3
IPv4-NFS-SiteA,Tag2,Tag4
...
-----------------------------
.NOTES
===========================================================================
Created on: 31/03/2017 11:16
Created by: Alessio Rocchi <[email protected]>
Organization: VMware
Filename: SetDatastoreTag.ps1
===========================================================================
#>
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true,
ValueFromPipeline = $true)]
[ValidateNotNullOrEmpty()]
[System.String]$csvFile,
[Parameter(Mandatory = $true,
ValueFromPipeline = $true)]
[ValidateNotNullOrEmpty()]
[String]$vCenter,
[Parameter(ValueFromPipeline = $true,
Position = 2)]
[AllowNull()]
[String]$Username,
[Parameter(Position = 3)]
[AllowNull()]
[String]$Password
)
Import-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue | Out-Null
class vcConnector : System.IDisposable
{
[String]$Username
[String]$Password
[String]$vCenter
[PSObject]$server
static [vcConnector]$instance
vcConnector($Username, $Password, $vCenter)
{
Import-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue | Out-Null
$this.Username = $Username
$this.Password = $Password
$this.vCenter = $vCenter
$this.connect()
}
vcConnector($vcCredential, $vCenter)
{
Import-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue | Out-Null
$this.vcCredential = $vcCredential
$this.vCenter = $vCenter
$this.connect()
}
[void] hidden connect()
{
try
{
if ([String]::IsNullOrEmpty($this.Username) -or [String]::IsNullOrEmpty($this.Password))
{
$vcCredential = Get-Credential
Connect-VIServer -Server $this.vCenter -Credential $this.vcCredential -WarningAction SilentlyContinue -ErrorAction Stop | Out-Null
}
else
{
Connect-VIServer -Server $this.vCenter -User $this.Username -Password $this.Password -WarningAction SilentlyContinue -ErrorAction Stop
}
Write-Debug("Connected to vCenter: {0}" -f $this.vCenter)
}
catch
{
Write-Error($Error[0].Exception.Message)
exit
}
}
[void] Dispose()
{
Write-Debug("Called Dispose Method of Instance: {0}" -f ($this))
Disconnect-VIServer -WarningAction SilentlyContinue -Server $this.vCenter -Force -Confirm:$false | Out-Null
}
static [vcConnector] GetInstance()
{
if ([vcConnector]::instance -eq $null)
{
[vcConnector]::instance = [vcConnector]::new()
}
return [vcConnector]::instance
}
}
class Content{
[System.Collections.Generic.List[System.String]]$availableTags
[System.Collections.Generic.List[System.String]]$elements
Content()
{
}
Content([String]$filePath)
{
if ((Test-Path -Path $filePath) -eq $false)
{
throw ("Cannot find file: {0}" -f ($filePath))
}
try
{
# Cast the Get-Content return type to Generic List of Strings in order to avoid fixed-size array
$this.elements = [System.Collections.Generic.List[System.String]](Get-Content -Path $filePath -ea SilentlyContinue -wa SilentlyContinue)
$this.availableTags = $this.elements[0].split(',')
# Delete the first element aka availableTags
$this.elements.RemoveAt(0)
}
catch
{
throw ("Error reading the file: {0}" -f ($filePath))
}
}
}
try
{
$vc = [vcConnector]::new($Username, $Password, $vCenter)
$csvContent = [Content]::new($csvFile)
Write-Host("Available Tags: {0}" -f ($csvContent.availableTags))
foreach ($element in $csvContent.elements)
{
[System.Collections.Generic.List[System.String]]$splittedList = $element.split(',')
# Get the Datastore Name
[System.String]$datastoreName = $splittedList[0]
# Removing Datastore Name
$splittedList.RemoveAt(0)
# Create a List of Tags which will be assigned to the Datastore
[System.Collections.Generic.List[PSObject]]$tagsToAssign = $splittedList | ForEach-Object { Get-Tag -Name $_ }
Write-Host("Tags to assign to Datastore: {0} are: {1}" -f ($datastoreName, $tagsToAssign))
# Get Datastore object by the given Datastore Name, first field of the the line
$datastore = Get-Datastore -Name $datastoreName -ea Stop
# Iterate the assigned Datastore Tags
foreach ($tag in ($datastore | Get-TagAssignment))
{
# Check if the current tag is one of the available ones.
if ($tag.Tag.Name -in $csvContent.availableTags)
{
# Remove the current assigned Tag
Write-Host("Removing Tag: {0}" -f ($tag))
Remove-TagAssignment -TagAssignment $tag -Confirm:$false
}
}
# Finally add the new set of tags to the Datastore
foreach ($tag in $tagsToAssign)
{
Write-Host("Trying to assign Tag: {0} to Datastore: {1}" -f ($tag.Name, $datastoreName))
# Assign the Tag
New-TagAssignment -Entity $datastore -Tag $tag
}
}
}
catch [VMware.VimAutomation.Sdk.Types.V1.ErrorHandling.VimException.VimException]
{
Write-Error("VIException: {0}" -f ($Error[0].Exception.Message))
exit
}
catch
{
Write-Error $Error[0].Exception.Message
exit
}
finally
{
# Let be assured that the vc connection will be disposed.
$vc.Dispose()
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.