http://pubs.vmware.com/vi3/sdk/ReferenceGuide/vim.VirtualMachine.html#reconfigure
I'm writing a script to update the VMX state for virtual machines to comply with some standards, but also want to backup their state in case they need to re restored. The ReconfigVM task works fine, and I can pull the state via ($VM | Get-View).config.extraconfig
What I can't figure out is how to remove items using VirtualMachineConfigSpec.
Let's say I update a value as such.
$ExtraOptions = #{
"isolation.device.connectable.disable"="true";
"log.rotateSize"="100000";
}
$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
Foreach ($Option in $ExtraOptions.GetEnumerator()) {
$OptionValue = New-Object VMware.Vim.optionvalue
$OptionValue.Key = $Option.Key
$OptionValue.Value = $Option.Value
$vmConfigSpec.extraconfig += $OptionValue
}
# ...
foreach($vm in $vms){
$vm.ReconfigVM($vmConfigSpec)
}
Now let's say I create a backup hashtable of the config spec for the values prior to changing them. Since these values did not exist prior to the update there's no value prior.
A $null throws an error.
"isolation.device.connectable.disable"=$null;
An empty "" does no update
"isolation.device.connectable.disable"="";
I could simply revert the Boolean values and set integer values to 0, but I'd rather just remove the value from the configuration.
The documentation states the following about extraconfig
Additional configuration information for the virtual machine. This describes a set of modifications to the additional options. An option is removed if the key is present but the value is not set or the value is an empty string. Otherwise, the key is set to the new value.
Configuration keys that would conflict with parameters that are explicitly configurable through other fields in the ConfigSpec object are silently ignored.
EDIT
Passing a whitespace character (i.e. " ") will set the value to false or 0.
Apparently you'd have to download the vmx file (where these options are stored), and then re-upload the modified file while the VM is powered off, and maybe also un-registered.
$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
$vmConfigSpec.extraconfig = New-Object VMware.Vim.optionvalue
$vmConfigSpec.extraconfig[0].Key="cloud.uuid"
$vmConfigSpec.extraconfig[0].Value=""
$vm.ReconfigVM($vmConfigSpec)
Taking the plus sign out along with value of nothing "" should remove the line from the vmx file.
Related
I'm looking to get an idea of how to go about this.
In a for each loop. Do you need to define the variables before using them in a hash table for TITLE,DEPARTMENT,MANANGER,OFFICE
Currently, this does not set anything for the users in the foreach statement.
Foreach ($userdata in $datafile) {
$SetADUserdetails = #{
Identity=$userdata.Adusername
Title = $userdata.title
Office=$userdata.office
Department=$userdata.department
Manager=$userdata.manager
}
Set-ADUser #SetADUserdetails # Need to add Domain Controller Parameter
}
This is how the datafile looks like.
GivenName,Surname,Office,Title,Path,Manager,Department,Adusername
I'm sure if I set the variable first and then match the variable to hash table keys, it going to work.
Please let me know your thought. I'm fair new to PowerShell. I tried searching the internet for the correct method. No luck. just want to double-check before I define variables.
Yes, variables are rendered at runtime. When you define the hashtable the key will have the variable assigned at the time the hashtable was defined, or the last value that was set for that key with =.
In your code example, the keys of $SetADUserdetails have no value because the $userdata Properties have no value. Make sure that $datafile has the correct entries you are expecting and confirm each iteration of $userdata has some value. Either $datafile is incorrect or you are referencing each column by the wrong column name.
Preface: Please tell me a better way of doing this if you know of any! Even if it changes everything.
I have a powershell script that works with Net-SNMP to store the current value of an OID, change it and then check and store the values. Right now the previous and new values are stored in their own variable. There are about 160 OIDs that I need to change on >1000 nodes in my environment.
For Example for Previous and New values:
$P_vpwrSystemTempCompensation_0 = & '.\SnmpGet.exe' -q -r:"$ip" -v:2c -c:public -o:.1.3.6.1.4.1.13858.2.2.1.0
$N_vpwrSystemTempCompensation_0 = & '.\SnmpGet.exe' -q -r:"$ip" -v:2c -c:public -o:.1.3.6.1.4.1.13858.2.2.1.0
I'm try to figure the best way to store these and output into an CSV. I would like it to be formatted liked this:
Each CSV will be saved with the name and IP of the node I'm hitting.
You can make an object like this in a loop, and that could be exported to csv:
$oid = 'vpwrSystemTempCompensation.0'
$prev = 2
$new = 4647
[pscustomobject]#{OID=$oid; Previous=$prev; New=$new}
OID Previous New
--- -------- ---
vpwrSystemTempCompensation.0 2 5647
By the way, you don't need the call operator to run smnpget, if you don't put it in quotes:
.\SnmpGet.exe -q -r:$ip -v:2c -c:public -o:.1.3.6.1.4.1.13858.2.2.1.0
In PowerShell I use the following LDAP query to retrieve the active directory properties of a host name:
$Filter = "(&(ObjectCategory=Computer)(ObjectClass=Computer)(CN=$ComputerName))"
if ($Found = ([ADSISEARCHER]$Filter).FindOne()) {
$Details = $Found.GetDirectoryEntry()
}
Once I have these properties I would like to check if the computer account is disabled. The following LDAP query is allowing me to do that:
$Filter = "(&(objectCategory=computer)(userAccountControl:1.2.840.113556.1.4.803:=2)(CN=$ComputerName))"
([ADSISEARCHER]$Filter).FindOne()
What I would like to know is, how can I avoid using the second LDAP query and use the variable $Details from the first LDAP query to obtain $True or $False if the computer account is disabled?
I found some extra information but I can't seem to put the pieces together.
Thank you for your help.
The information you're looking for is encoded in the userAccountControl of the directory entry object. However, the property contains an array with a numeric value, so you need to check if the "disabled" flag (numeric value 2) in the first array element is set:
$disabled = [bool]($Details.userAccountControl[0] -band 2)
I need to change the internalname for a sharepoint 2010 list item field. For some reason, our migration software renamed it during 2007->2010 migration and this field is referenced by other processes so we need the internalname back to the original. This field exists in over 200 lists in the migrated site so we need a means to do this programatically - powershell preferred.
$newInternalName = "yourInternalFieldName"
$displayName = "oldDisplayName"
$SPWebApp = Get-SPWebApplication "http://yourwebapp"
foreach (SPList $currList in $SPWebApp.Lists)
{
foreach (SPField $fld in $currList.Fields) #you could potentially use a different command here to get the field more efficiently
{
if ($fld.Name == $displayName)
{
#The boolean in the parameter list is for required/non-required field $currList.Fields.Add($newInternalName,Microsoft.SharePoint.SPFieldType.Text,$False)
foreach (SPListItem $item in $currList.Items)
{
$item[$newInternalName] = $item[$displayName]
$item[$newInternalName].Title = $displayName #I'm assuming you want to keep the display name the same
$item.Update()
}
Break #since you've already fixed the column for this list no need to keep going through the fields
# optional delete unwanted column
# $currList.Fields.Delete($displayName)
# $currList.Update()
}
}
}
As far as I know, you cannot change the internal name of a field once it has been created. Here I create a new field with the correct internal name and copy over the values. I don't have access to a server with Powershell today so I wasn't able to test this, but this should be very close to what you need. You may need to tweak it a bit based on what type of field you're dealing with, if you want to use a different Add function, and/or you want to delete the old field.
The enumeration to select the type of the field in the Add function is defined here:
http://msdn.microsoft.com/en-us/library/office/microsoft.sharepoint.spfieldtype(v=office.14).aspx
There are a couple of overloads for the Add function so if the one I used doesn't work for you, you might wanna use one of these others
http://msdn.microsoft.com/en-us/library/office/aa540133(v=office.14).aspx
I'm configuring a web site using PowerShell and I want to set the default document. How do I do this?
This is one way:
$metabasePath = "IIS://Localhost/W3SVC"
$iisNumber = "12345"
$site = new-object
System.DirectoryServices.DirectoryEntry("$metabasePath/$iisNumber/root")
$site.psbase.Properties["DefaultDoc"].Value =
"newdefdoc.htm," + $site.psbase.Properties["DefaultDoc"].Value
$site.psbase.CommitChanges()
The value returned in $site.psbase.Properties["DefaultDoc"].Value is a comma separated list of documents so you may need to re-jig the order to suit your case. The example above just adds a new default document (newdefdoc.htm) to the top of the list.