if (($signmail.IndexOf('#')) -ne $null)
{
$signname = $signmail.Remove($signmail.IndexOf('#'))
}
Else{
$signname = $signmail
}
Hi, basicaly, I try to see if the user enter his mail address completely, or just the first part. I try here to ask If the value after a # in the mail address is not empty, to erase it and put it in the new variable. If not, the variable give directly his name to the other variable. But it not work. I always receive the StartIndex can't be below 0 error.
Anyone think of a way to make this code work for that part ?
Thanks
From the String.IndexOf() documentation:
Reports the zero-based index of the first occurrence of a specified Unicode character or string within this instance. The method returns -1 if the character or string is not found in this instance.
So you'll want to test whether the return value is 0 or greater:
if ($signmail.IndexOf('#') -ge 0) {
$signname = $signmail.Remove($signmail.IndexOf('#'))
}
else {
$signname = $signmail
}
Related
I have a string variable in PowerShell which contains the value:
NFP|8dc3b47a-48eb-4696-abe2-48729beb63c8
I am attempting to get the beginning portion of that string into it's own variable by identifying the index of the "|" character and using a substring function to extract the first portion of the string, in this case "NFP". I am not sure how to escape the "|" so I can use it properly. It doesn't seem to recognize it at all. My latest attempt is as follows:
$PolicyManual = $Item["PolicyManual"]
write-host $PolicyManual #Displays NFP|8dc3b47a-48eb-4696-abe2-48729beb63c8
if ($PolicyManual.Contains([regex]::escape("|"))) {
$PolcyManual = $PolicyManual.Substring(0, $PolicyManual.IndexOf([regex]::escape("|")))
}
I'm sure this is simple, but I can't figure out how to make it work. Can anyone offer assistance to a PowerShell novice?
Thanks.
The problem is that .contains method doesn't know about regex and you are never entering the if condition because of this. When you do [regex]::escape("|"), the method is looking for a literal \|.
Try this instead:
$PolicyManual = "NFP|8dc3b47a-48eb-4696-abe2-48729beb63c8"
if ($PolicyManual.Contains('|')) {
$element0, $element1 = $PolicyManual.Split('|')
$element0 #=> NFP
$element1 #=> 8dc3b47a-48eb-4696-abe2-48729beb63c8
}
I have completed coding this same problem in python and was trying to generate a similar logic or atleast achieve a similar result in powershell.
Python Code-
def FRC(str):
h = {}
for ch in str:
if ch in h:
return ch
else:
h[ch] = 0
return '\0'
print(FRC("abcdedcba"))
I have tried a few possible codes and was able to only enumerate the array of characters to count their occurrences. Thank you for any suggestions.
Update1 - The code I have worked on is as follows:
function get-duplicatechar {
[CmdletBinding()]
param (
[string]$teststring
)
$hash = #()
$teststring = $teststring.ToCharArray()
foreach ($letter in $teststring)
{
if($letter -contains $hash){
return $letter
}else {
$hash = $hash + $letter
}
return "\0"
}
}
get-duplicatechar("saahsahh")
You could use the (.Net) HashSet class for this, which Add method (besides adding the value,) returns true if the element is added to the HashSet<T> object and false if the element is already present.
function get-duplicatechar {
[CmdletBinding()]
param (
[string]$str
)
$h = [System.Collections.Generic.HashSet[char]]::new()
foreach ($ch in $str.ToCharArray()) {
if(!$h.add($ch)) { return $ch }
}
}
Here's a working version using your code as base:
function get-duplicatechar {
[CmdletBinding()]
param (
[string]$teststring
)
$hash = #{}
$CharArray = $teststring.ToCharArray()
foreach ($letter in $CharArray) {
if($letter -in $hash.Keys) {
$letter
break
}
else {
$hash[$letter] = $null
}
}
}
One problem is that you are strongly typing $teststring to be a string, so when you add a character array later PowerShell just converts it into a string and thus $teststring remains a string (try $teststring.GetType() after $teststring = $teststring.ToCharArray() to see this for yourself).
One way to solve this is to do what I did and use a different variable for the character array. You could also solve it by changing the variable to a character array directly by replacing [string]$teststring with [char[]]$teststring, that way any strings input to the function will be automatically cast as a character array.
The next mistake is using -contains where you need -in. The letter doesn't contain the array, you're looking for the letter in the array, just like you did in Python.
You can drop the return keyword entirely, PowerShell does not need it. Any output in your function will be output from the function automatically.
You also call your collection variable "hash", but you made an array. I changed it to be an actual hashtable, just like in your Python code. I also changed the way we add to it to more closely reflect what you did in Python. There are many ways to do this, this is just one. Notice we'll need to add ".Keys" in our if-statement as well so we check for keys matching our letter.
I think that's it, ask if anything is unclear.
I try to Delete an phone number Entry within an contact while using the ExchangeWebService and powershell.
I can create new Contacts with Numbers and so on. I can even change those numbers. But i can set then to $null or "".
It always gives me Exception calling "Update" with "1" argument(s): "An object within a change description must contain one and only one property to modify."
I understand that im not allowed to set it to "" or null. But there have to be a way to delete a phone number entry.
So may be there is someone out there to help me with this issue.
So far i check if there is a change in the phone number and only update it where there is.
$enumBusinessPhoneValue = [Microsoft.Exchange.WebServices.Data.PhoneNumberKey]::BusinessPhone
if($c.PhoneNumbers[$enumBusinessPhoneValue] -ne "" -and $c.PhoneNumbers[$enumBusinessPhoneValue] -ne $null){
if($busPhone -ne ""){
if($c.PhoneNumbers[$enumBusinessPhoneValue] -ne $busPhone){
echo "="
$c.PhoneNumbers[$enumBusinessPhoneValue] = $busPhone
}
} else {
$c.PhoneNumbers[$enumBusinessPhoneValue] = ""
}
} else {
if($busPhone -ne ""){
$c.PhoneNumbers[$enumBusinessPhoneValue] = $busPhone
}
}
$c.Update([Microsoft.Exchange.WebServices.Data.ConflictResolutionMode]::AlwaysOverwrite)
The problem lies in this line $c.PhoneNumbers[$enumBusinessPhoneValue] = "" even if i put in $null i get the same error.
Greetings
skratter
You need to use the Extended Property for the Business Phone in this case (this is the same as for EmailAdddresses https://blogs.msdn.microsoft.com/emeamsgdev/2012/05/17/ews-managed-api-how-to-remove-email1-email2-email3-from-a-contact/) eg
$PidTagBusinessTelephoneNumber = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x3A08,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::String);
$c.RemoveExtendedProperty($PidTagBusinessTelephoneNumber)
$c.Update([Microsoft.Exchange.WebServices.Data.ConflictResolutionMode]::AlwaysOverwrite)
I have a usernameField. On initial state, the field is empty. If I log in successfully, and log back out, it remembers my username for me.
Trying to create a test case for iOS (in swift) that will clear out the field (use the clearText button) if the field has content and then enter a desired string. If it's empty, it needs to skip the clearText button action (since it doesn't exist when the field value is nil) and go straight to entering the username.
It always skips the if statement, even when it's true. Looks for the clearText button, and fails. Works if there's a value in the field, though.
Tried lots of different approaches, but here's my best current working code. Open to any suggestions, as I have no one to really help me learn this stuff. I'm sure I'm just missing something fundamental:
let staffusernameloginfield = app.scrollViews.otherElements.textFields["staffUsernameLoginField"]
staffusernameloginfield.tap()
func checkUsernameFieldContents() {
if staffusernameloginfield == (Content:nil) {
staffusernameloginfield.typeText("owner")
}
else {
elementsQuery.buttons["Clear text"].tap()
staffusernameloginfield.typeText("owner")
}
}
checkUsernameFieldContents()
Have also tried:
if staffusernameloginfield == ("") {
staffusernameloginfield.typeText("owner")
}
else {
elementsQuery.buttons["Clear text"].tap()
staffusernameloginfield.typeText("owner")
}
}
I know that I could hack it by having it always enter a value into the field and then clear it out and enter the desired value, but in this test case, I'm not trying to test for that.
I would compare the value (raw attribute of the element). This type can vary so I always do it as a string:
if staffusernameloginfield.value as! String == "" {
staffusernameloginfield.typeText("owner")
}
else {
elementsQuery.buttons["Clear text"].tap()
staffusernameloginfield.typeText("owner")
}
Now it should enter 'owner' if it sees there is no value in staffusernameloginfield.
to check if the textfield is empty:
app.textFields["signInEmailAddressTextFieldLabel"].value as? String == ""
if the textfield is empty, then that's true
I usually use an assert to check if the value is empty or not, in this example the value is equal to a variable incorrectPassword:
XCTAssertEqual(app.textFields["\(passwordSecureTextFieldLabel)"].value as? String, incorrectPassword)
for a user creation scrip in powershell i'm using textbox object to fill the information of the new user (family name, first name)
I return a value like that:
$TextlabelUsername.text = $Textbox1.text.ToString().Substring(0,5)
Which apply on a button click.
But using that methode if one of my string value is less then 5 caracters the script return an error that the string is not enough long.
Is there a way to select 5 or less caracters or an other method to process ?
Try this:
$str = $Textbox1.text.ToString()
$TextlabelUsername.text = $str.Substring(0, [math]::Min(5, $str.Length))
There is nothing wrong with Ansgar Wiecher's method. Here is an alternative though:
$TextLabelUserName.Text = $Textbox1.Text.ToString() -replace '(.{0,5}).*', '$1'