I was wondering how I can reference credential in CMD called by DSC.
This is the configuration that I'm trying to deploy, but it doesn't receive credentials.
configuration SQLCMD
{
param
(
[Parameter(Mandatory=$true)]
[ValidateNotNullorEmpty()]
[PSCredential]
$Credential
)
Import-DscResource -ModuleName xSqlServer
Node localhost
{
LocalConfigurationManager
{
ConfigurationMode = 'ApplyOnly'
RebootNodeIfNeeded = $true
ActionAfterReboot = 'ContinueConfiguration'
AllowModuleOverwrite = $true
}
Script DeployDBmoveTempDB
{
SetScript = {
$SourceFile = 'C:\DatabaseTest.dacpac'
$TargetServerName = 'localhost'
$TargetDatabaseName = 'TestDB1'
$databaseSizeSQLCMD = '200MB'
$databaseLogSizeSQLCMD = '20MB'
$tempdbSizeSQLCMD = '1900MB'
$tempdbLogSizeSQLCMD = '1900MB'
trap {
Write-Error $_
Exit 1
}
$args = #('/Action:Publish'
,"/SourceFile:$SourceFile"
,"/TargetServerName:$TargetServerName"
,"/TargetUser:$Credential.UserName"
,"/TargetPassword:$Credential"
,"/TargetDatabaseName:$TargetDatabaseName"
,"/v:databaseSizeSQLCMD=$databaseSizeSQLCMD"
,"/v:databaseLogSizeSQLCMD=$databaseLogSizeSQLCMD"
,"/v:tempdbSizeSQLCMD=$databaseSizeSQLCMD"
,"/v:tempdbLogSizeSQLCMD=$databaseLogSizeSQLCMD"
,'/p:BlockOnPossibleDataLoss=false'
)
try {
& "C:\Program Files (x86)\Microsoft SQL Server\130\DAC\bin\SqlPackage.exe" $args
}
catch {
Write-Host $_ ;
}
}
TestScript = {
Test-Path D:\TestDB1_primary.mdf
}
GetScript = { <# This must return a hash table #> }
}
}
}
However, the following configuration works fine:
configuration SQLCMD
{
param
(
[Parameter(Mandatory=$true)]
[ValidateNotNullorEmpty()]
[PSCredential]
$Credential
)
Import-DscResource -ModuleName xSqlServer
Node localhost
{
LocalConfigurationManager
{
ConfigurationMode = 'ApplyOnly'
RebootNodeIfNeeded = $true
ActionAfterReboot = 'ContinueConfiguration'
AllowModuleOverwrite = $true
}
Script DeployDBmoveTempDB
{
SetScript = {
$ErrorActionPreference = "Stop"
$SourceFile = 'C:\DatabaseTest.dacpac'
$TargetServerName = 'localhost'
$user_name = 'mySqlAdmin'
$user_pwd = 'blabla'
$TargetDatabaseName = 'TestDB1'
$databaseSizeSQLCMD = '200MB'
$databaseLogSizeSQLCMD = '20MB'
$tempdbSizeSQLCMD = '1900MB'
$tempdbLogSizeSQLCMD = '1900MB'
trap {
Write-Error $_
Exit 1
}
$args = #('/Action:Publish'
,"/SourceFile:$SourceFile"
,"/TargetServerName:$TargetServerName"
,"/TargetUser:$user_name"
,"/TargetPassword:$user_pwd"
,"/TargetDatabaseName:$TargetDatabaseName"
,"/v:databaseSizeSQLCMD=$databaseSizeSQLCMD"
,"/v:databaseLogSizeSQLCMD=$databaseLogSizeSQLCMD"
,"/v:tempdbSizeSQLCMD=$databaseSizeSQLCMD"
,"/v:tempdbLogSizeSQLCMD=$databaseLogSizeSQLCMD"
,'/p:BlockOnPossibleDataLoss=false'
)
try {
& "C:\Program Files (x86)\Microsoft SQL Server\130\DAC\bin\SqlPackage.exe" $args
}
catch {
Write-Host $_ ;
}
}
TestScript = {
Test-Path D:\TestDB1_primary.mdf
}
GetScript = { <# This must return a hash table #> }
}
}
}
DSC-configurations store scripts as strings in the generated mof and does not expand variables by default since it wouldn't know which to expand and which to keep as part of the script. However, by specifying the $using:-Scope, you can include variables defined in the configuration. During mof-compilcation, the variables are then added at the start of each of the Get-/Set-/TestScript scriptblocks.
Ex:
configuration SQLCMD
{
param
(
[Parameter(Mandatory=$true)]
[ValidateNotNullorEmpty()]
[PSCredential]
$Credential
)
Import-DscResource -ModuleName xSqlServer
$user_name = $Credential.UserName
$user_pwd = $Credential.GetNetworkCredential().Password
Node localhost
{
Script DeployDBmoveTempDB
{
SetScript = {
$TargetDatabaseName = 'TestDB1'
$args = #(,"/TargetUser:$using:user_name"
,"/TargetPassword:$using:user_pwd"
,"/TargetDatabaseName:$TargetDatabaseName")
try {
& "C:\Program Files (x86)\Microsoft SQL Server\130\DAC\bin\SqlPackage.exe" $args
}
catch { Write-Host $_ }
}
TestScript = { Test-Path "D:\TestDB1_primary.mdf" }
GetScript = { <# This must return a hash table #> }
}
}
}
Be aware that the password will be stored in plain text in the mof-file. Ex:
SetScript = "$user_name ='User1'\n$user_pwd ='Password1'\n \n\n $TargetDatabaseName = 'TestDB1'\n $args = #(,\"/TargetUser:$user_name\"\n
,\"/TargetPassword:$user_pwd\"\n ,\"/TargetDatabaseName:$TargetDatabaseName\") \n\n try {\n & \"C:\\Program File
s (x86)\\Microsoft SQL Server\\130\\DAC\\bin\\SqlPackage.exe\" $args\n }\n catch { Write-Host $_ } \n\n ";
Related
My Powershell script receives multiple Microsoft Office365 DSC policy configuration policy files as input in a folder as below:
file1.ps1
Configuration EXOSharingPolicy {
param (
[parameter()]
[System.Management.Automation.PSCredential]
$GlobalAdmin
)
if ($null -eq $GlobalAdmin) {
<# Credentials #>
$GlobalAdmin = Get-Credential -Message "Credentials"
}
else {
$Credential = $GlobalAdmin
}
$OrganizationName = $Credential.UserName.Split('#')[1]
Import-DscResource -ModuleName 'Microsoft365DSC' -ModuleVersion '1.22.907.1'
Node localhost
{
EXOSharingPolicy 8b39ae5a-f4ed-4bdb-932d-fbb9397f7fc6
{
Credential = $Credential;
Default = $True;
Domains = #("Anonymous:CalendarSharingFreeBusyReviewer");
Enabled = $True;
Ensure = "Present";
Name = "Default Sharing Policy";
}
}
}
file2.ps1
Configuration AADTenantDetails {
param (
[parameter()]
[System.Management.Automation.PSCredential]
$GlobalAdmin
)
if ($null -eq $GlobalAdmin) {
<# Credentials #>
$GlobalAdmin = Get-Credential -Message "Credentials"
}
else {
$Credential = $GlobalAdmin
}
$OrganizationName = $Credential.UserName.Split('#')[1]
Import-DscResource -ModuleName 'Microsoft365DSC' -ModuleVersion '1.22.907.1'
Node localhost
{
AADTenantDetails 5cfcabd5-9c82-4bed-9934-09e1cf20c71b
{
Credential = $Credential;
IsSingleInstance = "Yes";
MarketingNotificationEmails = #();
SecurityComplianceNotificationMails = #();
SecurityComplianceNotificationPhones = #();
TechnicalNotificationMails = #("admin#tech.net.au");
}
}
}
file3.ps1
Configuration EXOEmailAddressPolicy {
param (
[parameter()]
[System.Management.Automation.PSCredential]
$GlobalAdmin
)
if ($null -eq $GlobalAdmin) {
<# Credentials #>
$GlobalAdmin = Get-Credential -Message "Credentials"
}
else {
$Credential = $GlobalAdmin
}
$OrganizationName = $Credential.UserName.Split('#')[1]
Import-DscResource -ModuleName 'Microsoft365DSC' -ModuleVersion '1.22.907.1'
Node localhost
{
EXOEmailAddressPolicy a2188f3f-80d5-419c-b229-063fc2c18dbf
{
Credential = $Credential;
EnabledEmailAddressTemplates = #("SMTP:#$OrganizationName");
EnabledPrimarySMTPAddressTemplate = "#$OrganizationName";
Ensure = "Present";
ManagedByFilter = "";
Name = "Default Policy";
Priority = "Lowest";
}
}
}
I have several of these configuration files.
Is there a way in powershell to combine/merge these files so I end up with one file with all configurations like below.
Configuration CombinedPolicy {
param (
[parameter()]
[System.Management.Automation.PSCredential]
$GlobalAdmin
)
if ($null -eq $GlobalAdmin) {
<# Credentials #>
$GlobalAdmin = Get-Credential -Message "Credentials"
}
else {
$Credential = $GlobalAdmin
}
$OrganizationName = $Credential.UserName.Split('#')[1]
Import-DscResource -ModuleName 'Microsoft365DSC' -ModuleVersion '1.22.907.1'
Node localhost
{
EXOSharingPolicy 8b39ae5a-f4ed-4bdb-932d-fbb9397f7fc6
{
Credential = $Credential;
Default = $True;
Domains = #("Anonymous:CalendarSharingFreeBusyReviewer");
Enabled = $True;
Ensure = "Present";
Name = "Default Sharing Policy";
}
AADTenantDetails 5cfcabd5-9c82-4bed-9934-09e1cf20c71b
{
Credential = $Credential;
IsSingleInstance = "Yes";
MarketingNotificationEmails = #();
SecurityComplianceNotificationMails = #();
SecurityComplianceNotificationPhones = #();
TechnicalNotificationMails = #("jarrod#j-tech.net.au");
}
EXOEmailAddressPolicy a2188f3f-80d5-419c-b229-063fc2c18dbf
{
Credential = $Credential;
EnabledEmailAddressTemplates = #("SMTP:#$OrganizationName");
EnabledPrimarySMTPAddressTemplate = "#$OrganizationName";
Ensure = "Present";
ManagedByFilter = "";
Name = "Default Policy";
Priority = "Lowest";
}
}
}
So, in the combined configuration file I only need the section under Node localhost from each of the individual configuration files to be combined instead of entire file contents merged.
I need this so I can apply all the DSC configurations an office 365 tenancy at once instead of applying individual configurations.
Hope that makes sense.
The following script gets the desired combined file. You can only have the DSC files in the folder "PathToDSCFiles". This also only works if the given DSC files all have a format like given in the examples, because it just skips the 21 lines in the given scripts and continues from there. If you have other formats from your files, you will have to find a way to just select everything that comes after "Node Localhost".
$path = "PathToDSCFiles"
$PathToCombinedFile = "PathToCombinedFile"
$list = (Get-ChildItem -Path $path).Name
$first = $list | select -First 1
$SecondWord = (Get-Content $path\$first).Split(" ")[1]
((Get-Content $path\$first)[0]) -creplace($SecondWord,"CombinedPolicy") > $PathToCombinedFile
Get-Content $path\$first | select -skip 1 | select -skipLast 2 >> $PathToCombinedFile
foreach($file in ($list | select -skip 1)){
if ($file -eq ($list | select -skip 1)[-1]){
Get-Content -Path $path\$file | select -Skip 21 >> $PathToCombinedFile
}else{
Get-Content -Path $path\$file | select -Skip 21 | select -SkipLast 2 >> $PathToCombinedFile
}
}
I have a third party function which creates a profile for a server. When I create an array and assign to hash that is required by third party function it is working fine, but when I dynamically create an array and assign it I am getting error.
Have tried working with simple variable that has all values and have created and array also of these values statically. But when I dynamically create it fails.
Function New-Disk
{
Param (
[parameter(Mandatory = $false)]
[Array] $XXX_drivedata
)
if ($XXX_drivedata[3] -ieq "yes")
{
$boot_data = $TRUE;
}
else
{
$boot_data = $FALSE;
}
if ($XXX_drivedata[9] -ieq "yes")
{
$erase_data = $TRUE;
}
else
{
$erase_data = $FALSE;
}
$params1 = #{
name = $XXX_drivedata[0];
RAID = $XXX_drivedata[1];
numberofDrives = $XXX_drivedata[2];
driveType = $XXX_drivedata[5];
driveSelectionBy = $XXX_drivedata[6];
minDriveSize = $XXX_drivedata[7];
maxDriveSize = $XXX_drivedata[8];
eraseDataOnDelete = $erase_data;
bootable = $boot_data;
accelerator = $XXX_drivedata[4];
storageLocation = $XXX_drivedata[10]
}
$params = $params1.Clone()
foreach($item in $params1.GetEnumerator())
{
#if ([string]::IsNullOrWhiteSpace($item.Value) -or ($item.Value -ieq "null"))
if (!$item.value)
{
$params.Remove($item.Key)
}
}
try {
$logical_disk_create = New-<Function for disk> #params
if ($logical_disk_create)
{
$XXX_disk_create_status = "pass"
return $SCID_disk_create_status,$logical_disk_create.SasLogicalJBOD
}
}
catch {
Write-Error $_
$XXX_disk_create_status = "fail"
return $XXX_disk_create_status,$logical_disk_create
continue
}
}
#------------------------------------------------
#Attach local disk and JBOD to controller
#------------------------------------------------
Function New-Controller
{
Param (
[parameter(Mandatory = $true)]
[Array] $SCID_controller_detail,
[parameter(Mandatory = $true)]
[Array] $SCID_logicaldisk_detail
)
if ($SCID_controller_detail[1] -ieq "yes")
{
$initialize_data = $TRUE;
}
else
{
$initialize_data = $FALSE;
}
$params1 = #{controllerID = $XXX_controller_detail[0];initialize = $initialize_data;writeCache = $XXX_controller_detail[2];logicalDisk = $XXX_logicaldisk_detail}
$params = $params1.Clone()
foreach($item in $params1.GetEnumerator())
{
if ($item.key -ne "logicalDisk")
{
#if ([string]::IsNullOrWhiteSpace($item.Value))
if (!$item.value)
{
$params.Remove($item.Key)
}
}
}
try {
$logicaldisk_controller_create = New-<Function for disk controller> #params
if ($logicaldisk_controller_create)
{
$SCID_disk_create_status = "pass"
return $SCID_disk_create_status,$logicaldisk_controller_create
}
}
catch {
Write-Error $_
$SCID_disk_create_status = "fail"
return $SCID_disk_create_status,$logicaldisk_controller_create
continue
}
}
#--------------------------------------------------
#Create Server Profile
#--------------------------------------------------
Function New-ServerProfile
{
.......
#------------------------------------------------------
#Read local disk and JBOD details
#------------------------------------------------------
$SP_logical_disk_list = #()
$SP_logical_disk_list_controller = #()
$XXX_controllerdata = #("$($serverprofile.localStorages.integratedStorageController.controllerID)", "$($serverprofile.localStorages.integratedStorageController.reinitialize)", "$($serverprofile.localStorages.integratedStorageController.writeCache)")
if ($serverprofile.localStorages.integratedStorageController.logicalDrive)
{
foreach ($logicaldrive in $($serverprofile.localStorages.integratedStorageController.logicalDrive))
{
$XXX_drivedata = #("$($logicaldrive.name)", "$($logicaldrive.raidLevel)", "$($logicaldrive.physicalDrives)", "$($logicaldrive.boot)", "$($logicaldrive.accelarator)", "$($logicaldrive.driveTechnology)")
$logicaldisk_create = New-Disk -XXX_drivedata $XXX_drivedata
if ($logicaldisk_create[0] -ne "fail")
{
$SP_logical_disk_list += $logicaldisk_create[1]
$XXX_drivedata.Clear()
}
}
$logdisk_controller = New-Controller -XXX_controller_detail $SCID_controllerdata -XXX_logicaldisk_detail $SP_logical_disk_list
if ($logdisk_controller[0] -ne "fail")
{
$SP_logical_disk_list_controller += $logdisk_controller[1]
}
}
...........................
$LogicalDisk = New-<Fuctionfordisk> -Name "MyDisk" | New-<Function for disk controller> -Initialize
$LogicalDisks = New-<Fuctionfordisk> -Name "MyDisk" | New-<Function for disk controller> -Initialize
$logcaldr = #($LogicalDisk, $LogicalDisks)
$params1 = #{
....................
other parameters
logicalDisk = $SP_logical_disk_list_controller
}
$params = $params1.Clone()
foreach($item in $params1.GetEnumerator())
{
if ($item.key -ne "LogicalDisk|localStorage")
{
#if ([string]::IsNullOrWhiteSpace($item.Value) -or ($item.Value -ieq "null"))
if (!$item.value)
{
$params.Remove($item.Key)
}
}
}
$task = New-<Server Profile> #params | Wait-<task>
When I use localdr it is working fine but when I use $SP_logical_disk_list_controller for Storage I am getting error
The JSON sent in the request contained a unknown type where a different unknown type was required on line 1 near column 746. Correct the content of the JSON and try the request again.
I have even tried using $logdisk_controller[1] but still same error comes.
How do I download files from a remote server over a PSSession? I'm aware that PS5 introduced Copy-Item -FromSession, but both local and remote may be not running PS5. My files are also quite large so a simple Get-Content may be problematic.
You can read file on remote side as sequence of byte arrays, stream them thru remoting, and then assemble them back to file locally:
function DownloadSingleFile {
param(
[System.Management.Automation.Runspaces.PSSession] $FromSession,
[string] $RemoteFile,
[string] $LocalFile,
[int] $ChunkSize = 1mb
)
Invoke-Command -Session $FromSession -ScriptBlock {
param(
[string] $FileName,
[int] $ChunkSize
)
$FileInfo = Get-Item -LiteralPath $FileName
$FileStream = $FileInfo.OpenRead()
try {
$FileReader = New-Object System.IO.BinaryReader $FileStream
try {
for() {
$Chunk = $FileReader.ReadBytes($ChunkSize)
if($Chunk.Length) {
,$Chunk
} else {
break;
}
}
} finally {
$FileReader.Close();
}
} finally {
$FileStream.Close();
}
} -ArgumentList $RemoteFile, $ChunkSize | ForEach-Object {
$FileName = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($LocalFile)
$FileStream = [System.IO.File]::Create($FileName)
} {
$FileStream.Write($_, 0, $_.Length)
} {
$FileStream.Close()
}
}
I have created an app for back up and restore of computers. I also allows modification of ADObjects through the use of custom Profile.ps1 file. The app runs fine in the ISE with no errors and works properly no errors in Windows 7. However, when I try to run it in a newly imaged Windows 10 machine I get "Property Can Not Be Found" errors on all my object properties.
The thing is I can read all the properties when I fill comboboxes fine. The error only occurs when the form is submitted. I will attach 1 of the forms I am having a problem with. Again it runs fine in Windows 7, but not Windows 10.
Could this be a problem with Microsoft updates?
Also, yes, I am setting Set-ExecutionPolicy -Scope Process -ExecutionPolicy Unrestricted.
Error message:
The property 'company' cannot be found on this object. Verify that the
property exist and can be set.
+ $CO.company = $company
+ Categoryinfo :InvalidOperation: (:) [] RuntimeExeption
Code:
. \\iotsdsp01pw\installs$\MoveToOU\PcDeployment\Profile.ps1
#region Validation Functions
function Validate-IsEmail ([string]$Email) {
return $Email -match "^(?("")("".+?""#)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])#))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$"
}
function Validate-IsURL ([string]$Url) {
if ($Url -eq $null) {
return $false
}
return $Url -match "^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?$"
}
function Validate-IsName ([string]$Name, [int]$MaxLength) {
if ($MaxLength -eq $null -or $MaxLength -le 0) {
#Set default length to 40
$MaxLength = 40
}
return $Name -match "^[a-zA-Z''-'\s]{1,$MaxLength}$"
}
function Validate-IsIP ([string]$IP) {
return $IP -match "\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"
}
function Validate-IsEmptyTrim ([string]$Text) {
if ($text -eq $null -or $text.Trim().Length -eq 0) {
return $true
}
return $false
}
function Validate-IsEmpty ([string]$Text) {
return [string]::IsNullOrEmpty($Text)
}
function Validate-IsDate ([string]$Date) {
return [DateTime]::TryParse($Date, [ref](New-Object System.DateTime))
}
#endregion
$No_Load = {
$NewForm.Close()
#Initialize variables
$dateTime = Get-Date -Format G
$userName = (Get-WmiObject -Class Win32_ComputerSystem -Property UserName).UserName
$computerName = $env:computername
#Varables for display
$distinguishedName = (Get-dn computer cn $computerName)
$computerObject = (Get-ADObject $distinguishedName)
$organizationalUnit = (Get-ADObject "OU=Agencies, DC=state, DC=in, DC=us")
#Initialize Form Controls
$lblUserNameNewNo.Text = $userName
$lblComputerNameNewNo.Text = $computerName
$lblPhysicalLocationNewNo.Text = $computerObject.location
$txtBillingCodeNewNo.Text = $computerObject.departmentNumber
$comboboxAccountTypeNewNo.Text = $computerObject.extensionAttribute15
$comboboxOrganizationalUnitNewNo.Text = $computerObject.company
Load-ComboBox -ComboBox $comboboxOrganizationalUnitNewNo ($organizationalUnit.children | %{ $_.OU })
}
#region Control Helper Functions
function Load-ComboBox {
Param (
[ValidateNotNull()]
[Parameter(Mandatory = $true)]
[System.Windows.Forms.ComboBox]$ComboBox,
[ValidateNotNull()]
[Parameter(Mandatory = $true)]
$Items,
[Parameter(Mandatory = $false)]
[string]$DisplayMember,
[switch]$Append
)
if (-not $Append) {
$ComboBox.Items.Clear()
}
if ($Items -is [Object[]]) {
$ComboBox.Items.AddRange($Items)
} elseif ($Items -is [Array]) {
$ComboBox.BeginUpdate()
foreach ($obj in $Items) {
$ComboBox.Items.Add($obj)
}
$ComboBox.EndUpdate()
} else {
$ComboBox.Items.Add($Items)
}
$ComboBox.DisplayMember = $DisplayMember
}
#Validation
function ParameterValidate {
Param (
[Parameter(Mandatory = $true)]
[ValidateNotNull()]
[ValidateLength(1, 10)]
[String]$Text
)
return $true
}
$comboboxOrganizationalUnitNewNo_Validating = [System.ComponentModel.CancelEventHandler]{
#Check if the Name field is empty
$result = Validate-IsEmptyTrim $comboboxOrganizationalUnitNewNo.Text
if ($result -eq $true) {
#Mark a failure only if the Validation failed
$script:ValidationFailed = $true
#Display an error message
$errorprovider1.SetError($comboboxOrganizationalUnitNewNo, "Please select agency.");
} else {
#Clear the error message
$errorprovider1.SetError($comboboxOrganizationalUnitNewNo, "");
}
}
$txtBillingCodeNewNo_Validating = [System.ComponentModel.CancelEventHandler]{
#Check if the Name field is empty
$result = Validate-IsEmptyTrim $txtBillingCodeNewNo.Text
if ($result -eq $true) {
#Mark a failure only if the Validation failed
$script:ValidationFailed = $true
#Display an error message
$errorprovider1.SetError($txtBillingCodeNewNo, "Please enter billing code.");
} else {
#Clear the error message
$errorprovider1.SetError($txtBillingCodeNewNo, "");
}
}
$comboboxAccountTypeNewNo_Validating = [System.ComponentModel.CancelEventHandler]{
$result = Validate-IsEmptyTrim $comboboxAccountTypeNewNo.Text
if ($result -eq $true) {
#Mark a failure only if the Validation failed
$script:ValidationFailed = $true
#Display an error message
$errorprovider1.SetError($comboboxAccountTypeNewNo, "Please enter agency type.");
} else {
#Clear the error message
$errorprovider1.SetError($comboboxAccountTypeNewNo, "");
}
}
$control_Validated = {
#Pass the calling control and clear error message
$errorprovider1.SetError($this, "");
}
$No_FormClosing = [System.Windows.Forms.FormClosingEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.FormClosingEventArgs]
#Validate only on OK Button
if ($No.DialogResult -eq "OK") {
#Init the Validation Failed Variable
$script:ValidationFailed = $false
#Validate the Child Control and Cancel if any fail
$No.ValidateChildren()
#Cancel if Validation Failed
$_.Cancel = $script:ValidationFailed
}
}
#Events
$buttonColor_Click = {
#TODO: Place custom script here
$colordialog1.ShowDialog()
}
$linklblViewListNewNo_LinkClicked = [System.Windows.Forms.LinkLabelLinkClickedEventHandler]{
Start-Process "http://billingcodes.iot/"
}
$btnSubmitNewNo_Click = {
#TODO: Place custom script here
$company = $comboboxOrganizationalUnitNewNo.Text
$departmentNumber = $txtBillingCodeNewNo.Text
$accountType = $comboboxAccountTypeNewNo.Text
if ($accountType -eq "Seat") {
$accountType = " "
}
#Varables for Set-ADObject
$computerObject.company = $company
$computerObject.departmentNumber = $departmentNumber
$computerObject.extensionAttribute15 = $accountType
try {
$computerObject.SetInfo()
[Environment]::Exit(1)
} catch {
$labelDialogRedNewNo.Text = "AD computer object not found"
}
}
This is your culprit (from what I can see):
$No_Load = {
...
$computerObject = (Get-ADObject $distinguishedName)
...
}
...
$btnSubmitNewNo_Click = {
...
$computerObject.company = $company
...
}
You assign a computer object to the variable $computerObject in one scriptblock, and try to change one of its properties in another scriptblock. However, to be able to share a variable between scriptblocks you need to make it a global variable, otherwise you have two separate local variables that know nothing about each other.
$No_Load = {
...
$global:computerObject = Get-ADObject $distinguishedName
...
}
...
$btnSubmitNewNo_Click = {
...
$global:computerObject.company = $company
...
}
BTW, I doubt that this ever worked in Windows 7, since it failed with the same error on my Windows 7 test box.
I'm attempting to create a script cmdlet with dynamic parameters for each of the targets in an MSBuild project file. It's working with one minor annoyance - it only autocompletes the static parameters until I type the first character(s) of a target parameter.
My script with the cmdlet "Make-Project" follows.
What would cause DynamicParam to not return parameters unless the first part of the parameter is entered?
Set-Alias mk Make-Project
function Make-Project
{
[CmdletBinding(DefaultParameterSetName="Build")]
PARAM(
[Parameter(ParameterSetName="Build")]
[Switch]$Build,
[Parameter(ParameterSetName="Clean")]
[Switch]$Clean,
[Parameter(ParameterSetName="Rebuild")]
[Switch]$Rebuild,
[ValidateSet( "q","quiet", "m","minimal", "n","normal", "d","detailed", "diag","diagnostic" )]
[string]$BuildVerbosity,
[Switch]$CertificationBuild,
[Switch]$BuildDebug
)
PROCESS
{
# Defaults
$certBuild = ""
$target = "Usage"
$buildTarget = "Build"
$verbosity = "minimal"
$configuration = "release"
if ( [System.Convert]::ToBoolean( $env:project_build_debug ) )
{ $configuration = "debug" }
foreach( $paramName in $MyInvocation.BoundParameters.Keys )
{
switch -RegEx ( $paramName )
{
"(?i)CertificationBuild" { $certBuild = "cert" }
"(?i)^(Build|Clean|Rebuild)$" { $buildTarget = $paramName }
"(?i)^BuildVerbosity$" { $verbosity = $MyInvocation.BoundParameters[ $paramName ] }
"(?i)^BuildDebug$" { $configuration = "debug" }
default { $target = $paramName }
}
}
$msbuildexe = Get-MSBuildExe
if ( $msbuildexe.Contains( "v4.0" ) )
{ $cmd = "$msbuildexe /v:$verbosity $certBuild /m /property:Configuration=$configuration /property:BuildTarget=$buildTarget /target:$target /property:CLR4=1 Project.proj" }
else
{ $cmd = "$msbuildexe /v:$verbosity $certBuild /m /property:Configuration=$configuration /property:BuildTarget=$buildTarget /target:$target /tv:3.5 Project.proj" }
Write-Host $cmd
Invoke-Expression $cmd
}
DynamicParam
{
$projFile = '.\Project.Proj'
$projXml = [xml]( Get-Content $projFile )
$paramDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
$projXml.Project.Target | % {
$paramName = $_.Name
$attribute = New-Object System.Management.Automation.ParameterAttribute
$attribute.ParameterSetName = "__AllParameterSets"
$attribute.Mandatory = $false
$attributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
$attributeCollection.Add($attribute)
$param = New-Object System.Management.Automation.RuntimeDefinedParameter( $paramName, [Switch], $attributeCollection )
$paramDictionary.Add( $paramName, $param )
}
return $paramDictionary
}
}
function Get-MSBuildExe
{
$bitness = ""
if ( $env:PROCESSOR_ARCHITECTURE -eq "AMD64" )
{ $bitness = "64" }
$msbuildexe = "$env:SystemRoot\Microsoft.NET\Framework${bitness}\v4.0.30319\MSBuild.exe"
if ( -not (Test-Path $msbuildexe) )
{ $msbuildexe = "$env:SystemRoot\Microsoft.NET\Framework${bitness}\v3.5\MSBuild.exe" }
$msbuildexe
}
I would suggest that you not add a bunch of switch parameters. Instead, add one dynamic parameter named "Target" and add a ValidateSet attribute to it with the list of targets.
This is a helpful article.
http://robertrobelo.wordpress.com/2010/02/12/add-parameter-validation-attributes-to-dynamic-parameters/