Hi,
I am creating a script to retrieve the user's profil in a custom WMI class. But I get a failure on set-wmiclass...
Set-WMIInstance : Tentative de placement d’une instance sans clé définie.
Au caractère Ligne:1 : 1
+ Set-WMIInstance -class $Str_ClassName -argument $arglist
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation : (:) [Set-WmiInstance], ManagementException
+ FullyQualifiedErrorId : SetWMIManagementException,Microsoft.PowerShell.Commands.SetWmiInstance
#Create Class
<#
Function New-WMI-Class {
$newClass = New-Object System.Management.ManagementClass("root\cimv2", [String]::Empty, $null);
$newClass["__CLASS"] = "ProfilSize";
$newClass.Properties.Add("Name", [System.Management.CimType]::String, $false)
$newClass.Properties["ID"].Qualifiers.Add("key", $true)
$newClass.Properties["ID"].Qualifiers.Add("read", $true)
$newClass.Properties.Add("Nombre", [System.Management.CimType]::String, $false)
$newClass.Properties["Name"].Qualifiers.Add("read", $true)
$newClass.Properties.Add("Taille", [System.Management.CimType]::String, $false)
$newClass.Properties["State"].Qualifiers.Add("read", $true)
$newClass.Put()
}
#>
Function New-WMIClass {
<#
.SYNOPSIS
This function help to create a new WMI class.
.DESCRIPTION
The function allows to create a WMI class in the CimV2 namespace.
Accepts a single string, or an array of strings.
.PARAMETER ClassName
Specify the name of the class that you would like to create. (Can be a single string, or a array of strings).
.PARAMETER NameSpace
Specify the namespace where class the class should be created.
If not specified, the class will automatically be created in "Root\cimv2"
.EXAMPLE
New-WMIClass -ClassName "PowerShellDistrict"
Creates a new class called "PowerShellDistrict"
.EXAMPLE
New-WMIClass -ClassName "aaaa","bbbb"
Creates two classes called "aaaa" and "bbbb" in the Root\cimv2
.NOTES
Version: 1.0
Author: Stephane van Gulick
Creation date:16.07.2014
Last modification date: 16.07.2014
.LINK
www.powershellDistrict.com
.LINK
http://social.technet.microsoft.com/profile/st%C3%A9phane%20vg/
#>
Param(
[Parameter(Mandatory=$true,valueFromPipeLine=$true)][string[]]$ClassName,
[Parameter(Mandatory=$false)][string]$NameSpace = "root\cimv2"
)
foreach ($NewClass in $ClassName){
if (!(Get-WMIClass -ClassName $NewClass -NameSpace $NameSpace)){
write-verbose "Attempting to create class $($NewClass)"
$WMI_Class = ""
$WMI_Class = New-Object System.Management.ManagementClass($NameSpace, $null, $null)
$WMI_Class.name = $NewClass
$WMI_Class.Put() | out-null
write-host "Class $($NewClass) created."
}else{write-host "Class $($NewClass) is already present. Skiping.."}
}
}
Function New-WMIProperty {
<#
.SYNOPSIS
This function help to create new WMI properties.
.DESCRIPTION
The function allows to create new properties and set their values into a newly created WMI Class.
Event though it is possible, it is not recommended to create WMI properties in existing WMI classes !
.PARAMETER ClassName
Specify the name of the class where you would like to create the new properties.
.PARAMETER PropertyName
The name of the property.
.PARAMETER PropertyValue
The value of the property.
.EXAMPLE
New-WMIProperty -ClassName "PowerShellDistrict" -PropertyName "WebSite" -PropertyValue "www.PowerShellDistrict.com"
.NOTES
Version: 1.0
Author: Stephane van Gulick
Creation date:16.07.2014
Last modification date: 16.07.2014
.LINK
www.powershellDistrict.com
.LINK
http://social.technet.microsoft.com/profile/st%C3%A9phane%20vg/
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)]
[ValidateScript({
$_ -ne ""
})]
[string]$ClassName,
[Parameter(Mandatory=$false)]
[string]$NameSpace="Root\cimv2",
[Parameter(Mandatory=$true)][string]$PropertyName,
[Parameter(Mandatory=$false)][string]$PropertyValue=""
)
begin{
[wmiclass]$WMI_Class = Get-WmiObject -Class $ClassName -Namespace $NameSpace -list
}
Process{
write-verbose "Attempting to create property $($PropertyName) with value: $($PropertyValue) in class: $($ClassName)"
$WMI_Class.Properties.add($PropertyName,$PropertyValue)
Write-Output "Added $($PropertyName)."
}
end{
$WMI_Class.Put() | Out-Null
[wmiclass]$WMI_Class = Get-WmiObject -Class $ClassName -list
return $WMI_Class
}
}
[String]$Str_ClassName="ProfilSize"
[Array]$Arr_Property=@("Name","Compte","Total")
#Création de la classe
New-WMIClass -ClassName "$Str_ClassName"
#On créer les propriétés
ForEach ($Prop in $Arr_Property) {
New-WMIProperty -ClassName "$Str_ClassName" -PropertyName "$Prop"
# -PropertyValue "www.PowerShellDistrict.com"
}
[array]$Arr_users = Get-ChildItem -Path "$envsystemdrive\Users"
$Arr_users | ForEach-Object {
$colItems = (Get-ChildItem $env:systemdrive\Users\$($_.Name) -recurse -Force -ErrorAction SilentlyContinue | Measure-Object -property length -sum)
$DirectorySizeMB = [math]::Round($colItems.sum / 1MB,1)
Write-host "$($_.Name): $DirectorySizeMB Mo"
$arglist = @{
Name = $($_.Name)
Compte = $($colItems.Count).ToString()
Total = $($colItems.sum).ToString()
}
Set-WmiInstance -$Str_ClassName -Arguments $arglist
}