Set EntityReference attribute to not working Microsoft.Xrm.Data.PowerShell - powershell

I cannot update a theme by PowerShell using Microsoft.Xrm.Data.PowerShell.
The following code is not working:
#Get Theme
$res = Get-CrmRecords -Conn $connection -EntityLogicalName theme -FilterAttribute name -FilterOperator eq -FilterValue $nameOfTheme -Fields themeid, name, logoid, isdefaulttheme, logotooltip, navbarbackgroundcolor, navbarshelfcolor, headercolor, globallinkcolor, selectedlinkeffect, hoverlinkeffect, processcontrolcolor, defaultentitycolor, defaultcustomentitycolor, controlshade, controlborder
$ImportedTheme = $res.CrmRecords[0]
#Get Resource web
$resLogo = Get-CrmRecords -Conn $connection -EntityLogicalName webresource -FilterAttribute name -FilterOperator eq -FilterValue "LogoName"
$ImportedLogo = $resLogo.CrmRecords[0]
$ERLogo = (New-CrmEntityReference -EntityLogicalName webresource -Id $ImportedLogo.webresourceid)
#Update Theme
$ImportedTheme.logoid = $ERLogo
Set-CrmRecord -Conn $connection -CrmRecord $ImportedTheme
This will given an error:
Impossible d'appeler une méthode dans une expression Null.
Au caractère C:\Users\il...ll\Microsoft.Xrm.Data.PowerShell.psm1:628 : 16
+ if($crmFieldValue -eq $null)
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation : (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Le cast spécifié n'est pas valide. Au caractère
C:\Users\il...ll\Microsoft.Xrm.Data.PowerShell.psm1:773 : 9
+ throw $conn.LastCrmException
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], InvalidCastException
+ FullyQualifiedErrorId : Le cast spécifié n'est pas valide.
I think the problem comes from this verification in this part of code of Xrm.Data.PowerShell:
if($crmFieldValue -eq $null) { Write-Host "$crmFieldValue -eq $null"
$newfield.Type = [Microsoft.Xrm.Tooling.Connector.CrmFieldType]::Raw $value = $null }
I cannot find a solution. Do you have any idea?

Related

PowerShell Output English

I read English Output
, where it gives this function to force PowerShell output in English
function Set-PowerShellUICulture {
param([Parameter(Mandatory=$true)]
[string]$Name)
process {
$culture = [System.Globalization.CultureInfo]::CreateSpecificCulture($Name)
$assembly = [System.Reflection.Assembly]::Load("System.Management.Automation")
$type = $assembly.GetType("Microsoft.PowerShell.NativeCultureResolver")
$field = $type.GetField("m_uiCulture", [Reflection.BindingFlags]::NonPublic -bor [Reflection.BindingFlags]::Static)
$field.SetValue($null, $culture)
}
}
This only seems to work half-way, as line 2 here contains Norwegian "Ingen Tilgang", which means "Access Denied".
get-acl *
get-acl : Ingen tilgang
At line:1 char:1
+ get-acl *
+ ~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-Acl], UnauthorizedAccessException
+ FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.GetAclCommand
Any other way I can force output to be en English?;)

POWERSHELL - Define a default parameter set name ONLY if a parameter is specified

I have this code here (I simplified) :
Enum Region {
AU
BE
BR
CA
}
Function Get-AccByCountry
{
[CmdletBinding(DefaultParameterSetName='With_Specific_Countries')]
PARAM (
[Parameter(Mandatory=$false,
ParameterSetName='With_Specific_Countries')]
[Region[]]
$Regions,
[Parameter(Mandatory=$false,
ParameterSetName='Without_Specific_Countries')]
[Region[]]
$NotRegions
)
#BEGIN{}
PROCESS
{
Write-Verbose -Message "Starting global process"
IF($Regions){
foreach($r in $Regions){
[String]$r = $r
Get-AccByCountryLocalized $r -Verbose
}
}ELSEIF($NotRegions){
foreach($PR in [Enum]::GetValues([type]"Region")){
[String]$PR = $PR
IF($NotRegions -notcontains $PR)
{
$PR
Get-AccByCountryLocalized $PR -Verbose
}
}
}ELSE{
Get-AccByCountryGlobal -Verbose
}
}
#END{}
}
So I can call Get-AccByCountry either with no parameters specified, regions specified, of the regions I don't want specified (3 possibilities).
These 3 calls work :
Get-AccByCountry
Get-AccByCountry -Region AU,BE
Get-AccByCountry -NotRegions AU,BE
But when I try :
Get-AccByCountry AU,BE
It doesn't work ! It gives me this error (in French) :
Get-AccByCountry : Impossible de trouver un paramètre positionnel acceptant l'argument « System.Object[] ».
Au caractère Ligne:1 : 1
+ Get-AccByCountry CA,AU
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument : (:) [Get-AccByCountry], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Get-AccByCountry
I specified the DefaultParameterSetName, but it's not taken into account.
I want that my function to take the set 'With_Specific_Countries' everytime I give a parameter without specifying a set.
How should I do ?

Invoke-Command create new object

I've got a problem to create new object with Invoke-Command:
$a = 'TEST'
Invoke-Command -ComputerName $computer -Credential $cred -ScriptBlock {
param($a, $b, $c)
$o = New-Object -ComObject Outlook.Application
echo $a
} -ArgumentList ($a, $b, $c)
I'm getting the following error:
La récupération de la fabrique de classes COM pour le composant avec le CLSID
{0006F03A-0000-0000-C000-000000000046} a échoué en raison de l'erreur suivante :
80080005 Échec de l'exécution du serveur (Exception de HRESULT : 0x80080005
(CO_E_SERVER_EXEC_FAILURE)).
+ CategoryInfo : ResourceUnavailable: (:) [New-Object], COMException
+ FullyQualifiedErrorId : NoCOMClassIdentified,Microsoft.PowerShell.Commands.NewObjectCommand
TEST
where the trailing "TEST" is the output of echo $a.
I can't create the new object remotly. can you help me?

Powershell named parameters error

I'm tring to do the following:
$Feature = "OWAEnabled"
Set-CasMailbox Admin -$($Feature) $False
But i get each time this error:
Set-CASMailbox : Es wurde kein Positionsparameter gefunden, der das Argument "-OWAEnabled:True" akzeptiert.
In Zeile:1 Zeichen:2
+ set-casmailbox Administrator -$($Feature):$True
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-CASMailbox], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.Exchange.Management.RecipientTasks.SetCASMailbox
Can someone help me?
Thanks a lot
This will work, though not the prettiest:
$Feature = "OWAEnabled"
$c = "Set-CasMailbox Admin -$($Feature):`$False"
Invoke-Expression $c

What is the need of using 'put' method to set the value of a property through powershell

I tried the following cases :
$Localhst = $env:ComputerName;
$comp = [ADSI]('WinNT://'+$Localhst+',computer');
$user = $comp.Create('User', 'account24');
$user.SetPassword('Welcome1$');
$user.Description = "Created through powershell script client";
$user.SetInfo();
I am getting the following exception :
Error Occurred while executing powershell command Exception calling "SetInfo" with "0" argument(s): "The account already exists. (Exception from HRESULT: 0x8007
08B0)"
But when I am using put method it is working fine :
$Localhst = $env:ComputerName;
$comp = [ADSI]('WinNT://'+$Localhst+',computer');
$user = $comp.Create('User', 'account24');
$user.SetPassword('Welcome1$');
$user.put("Description","Created through powershell script client");
$user.SetInfo();
What is the difference between using put method to set the value and setting the value directly?
I use the same code but receive a different error
$Localhst = $env:ComputerName;
$comp = [ADSI]('WinNT://'+$Localhst+',computer');
$user = $comp.Create('User', 'account24');
$user.SetPassword('Welcome1$');
$user.Description = "Created through powershell script client";
$user.SetInfo();
Produce
$user.SetInfo();
Exception lors de la définition de « Description » : « Impossible de définir la propriété Value de l'objet PSMemberInfo de type « System.Management.Automation.PSMethod ». »
Au caractère Ligne:5 : 1
+ $user.Description = "Created through powershell script client";
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
+ FullyQualifiedErrorId : ExceptionWhenSetting
Exception lors de l'appel de « SetInfo » avec « 0 » argument(s) : « Accès refusé.
 »
Au caractère Ligne:6 : 1
+ $user.SetInfo();
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : CatchFromBaseAdapterMethodInvokeTI
The message is in French but it significate unable to define the property like this. But if I run it as administrator it create the user (with no description)