For a long time i try to simplize process of giving variable and save it with name of the variable and value ,but i didn’t seems to find a way to do so
using namespace System.Xml.Linq
BeforeAll{
class ADvar {
[string]$name
[string]$value
[string]$description
[string]$path = "$psscriptroot\xml\advar.xml"
[void] DoInit([pscustomobject]$pscustomobject){
$pscustomobjectName = (($pscustomobject) | Get-Member -Type NoteProperty).Name
foreach ($propertyName in $pscustomobjectName) {
$this.$propertyName = $pscustomobject.$propertyName
}
}
ADvar ([pscustomobject]$pscustomobject){
$this.DoInit($pscustomobject)
}
[object] save(){
$object = [XElement]::new("object",
[XAttribute]::new("name","advar"),
[XElement]::new("property",[XAttribute]::new("name","name"),$this.name),
[XElement]::new("property",[XAttribute]::new("name","value"),$this.value),
[XElement]::new("property",[XAttribute]::new("name","description"),$this.description)
)
return $object.ToString()
}
[string] GetVariableName([object]$variable) {
$variableName = (Get-Variable | Where-Object { $_.Value -eq $variable }).Name
return $variableName
}
}
}
Describe "it test ADvar" {
context "test first"{
it "test the getvariableName"{
$advar = [ADvar]::new([pscustomobject]@{
name = "test"
value = "test"
description = "test"
})
$advar.GetVariableName($advar) | Should -Be "advar"
}
}
}
powershell > test.advar.ps1
Starting discovery in 1 files.
Discovery found 1 tests in 10ms.
Running tests.
[-] it test ADvar.test first.test the getvariableName 15ms (14ms|1ms)
Expected strings to be the same, but they were different.
Expected length: 5
Actual length: 58
Strings differ at index 0.
Expected: 'advar'
But was: '? advar IsCoreCLR IsWindows isWindows10 this true variable'
^
at $advar.GetVariableName($advar) | Should -Be "advar", C:\Users\Adminnistrator\my-modules\run\test.adclass.ps1:49
at <ScriptBlock>, C:\Users\Adminnistrator\my-modules\run\test.adclass.ps1:49