Is there a way to check if a string starts with a string?
We are checking the groupmembership from the AD user. Our AD groups look like this: S_G_share1_W
The script for connecting the networkshares should only run if the groupname starts with "S_G_"
, because we have some other groups too.
$GroupArray = Get-ADPrincipalGroupMembership $env:USERNAME | select samaccountname foreach ($Group in $GroupArray) { if ($Group.StartsWith("S_G_")) { $Group = $Group -replace "S_G_", $FileServerRV Write-Host $Group $Group = $Group.Substring(0, $Group.Length-2) Write-Host $Group #erstellen des Anzeigennames $Groupname = $Group.Replace($FileServerRV, "") Write-Host "Call Function with parameter "$Group $Groupname } }
% is an alias for the ForEach-Object cmdlet. An alias is just another name by which you can reference a cmdlet or function.
If you want to find the string at certain positions within the string, use ^ to indicate the beginning of the string and $ to indicate the end of the string. To match the entire string, use both. Note the ^ at the start and the $ at the end to ensure that the entire input string must match.
The syntax of If statements in PowerShell is pretty basic and resembles other coding languages. We start by declaring our If statement followed by the condition wrapped in parentheses. Next, we add the statement or command we want to run if the condition is true and wrap it in curly brackets.
The Split operator splits one or more strings into substrings. You can change the following elements of the Split operation: Delimiter. The default is whitespace, but you can specify characters, strings, patterns, or script blocks that specify the delimiter.
$Group
is an object, but you will actually need to check if $Group.samaccountname.StartsWith("string")
.
Change $Group.StartsWith("S_G_")
to $Group.samaccountname.StartsWith("S_G_")
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With