User Profile
John_Dodo
Brass Contributor
Joined 3 years ago
User Widgets
Recent Discussions
struggling with parralele tasks
Hi, I'm trying to improve my code by using parrallele tasks. I'm struggling with testing the connections. $cpt = get-adcomputers -filter * $s = New-PSSession -ComputerName $Srv_list.DNSHostName $WSUSjob = Invoke-Command -session $s { write-host $(hostname) } } -JobName WSUSjob -AsJob $j = Get-Job $results = $j | Receive-Job I would know how to test a connection with a foreach and a try catch on a SINGLE computer, but what would be the right way of handling the result of the New-Pssession when using it directly on an ARRAY ? In my example it works and open sessions on answering computers. But I don't know how to retrieve the ones that didn't answer. Thank you.397Views0likes1CommentRun Get-Aduser from Get-Adgroupmember with users from sub-domains
Hello, I running is script that retrieves users from a group in ad.local The users within can be from ad.local and/or subdomain1.ad.local, subdomain2.ad.local etc When I run (from a computer that is part of subdomain1.ad.local) $userlist = get-adgroupmember -Identity $object_name -Server $object_ad -Credential $mycreds foreach ($user in$userlist){ $userad= Get-ADUser -Identity $($user.distinguishedName) } =>I getGet-ADuser : A referral was returned from the server I tried this : Get-ADUser -Identity $($user.distinguishedName)-server "usersdomain"-Credential $mycreds It works. But get-adgroupmember only returns distinguishedName : CN=jdoe,OU=Users,DC=subdomain1,DC=ad,DC=local name : jdoe objectClass : user objectGUID : 2293fd35-9fa7-4acc-a22f-5799f6dd2369 SamAccountName : jdoe SID : S-1-5-21-1231254564-630871074-310601177-1324 Do you know how I can retrieve the users domain (ex : "subdomain1.ad.local" or "ad.local" etc) without having to manipulatedistinguishedName? Thank you for your help.2KViews0likes1Commentretrieve rows with highest values in from array
Hi, I'm struggling a bit with something that should be easy ... I import a CSV and then I want to retrieve the rows with highest value for different "families". Ex: col1;col2;col3;col4 dateA;child1;son1;4 dateB;child2;son2;1 dateC;child2;son1;5 dateD;child1;son1;2 dateE;child1;son3;4 dateF;child1;son3;3 dateG;child2;son2;4 In here, I would like to get an new array with only col2;col3;col4 familly1;child1;4 familly1;child3;4 familly2;child2;4 familly2;child1;5 I have tried doing a foreach in a foreach but I'm still struggling with the two level depth of family and child Here is what I started to do : $tab= Import-Csv -Path $file-Delimiter ";" -Header "col1","col2", "col3" #This sort is probably not needed but I'm trying... $tabsorted = $tab| sort-object -property @{expression = "col2"; Descending = $true }, @{expression = "col3"; Descending = $true }, @{expression = "col4"; Descending=$true} $dummyfamilies= $tab| Select-Object -property "col2","col3" -Unique $tabtemp = @{} $count = 0 foreach($famillyin $dummyfamilies) { foreach ($row in $tabsorted){ #this is the painful part if( (( $row.col2 = $family.col2) -and $row.col3 = $family.col3) -and #here i'm lost ($tabtemp.col2.col3.Maxvalue -gt $family.col4)){ $tabtemp = @{family = $row.col2; son = $family.col3; Maxvalue = $row.col4} # something like this... } } } Well something like this. Thank you for your help.748Views0likes1CommentStruggling with double hop and credential management
Hi all, I have a setup where I use a powershell host to run scripts. These scripts are triggered from vmware Orchestrator and the scripts are manipulating files on a remote share. vRo => PSH => RemoteShare Therfor I am in a double hop situation and I need to handles the credentials. I use this solution :How to share credentials between different users & machines with PowerShell | PDQ When doing my tests, I run the script with my personal user account. I use a Fct_ConnectToADfunction to create a PSCredentials object, using a service account and the -key option to be able to run the script with any user from any computer (works fine) and still generate the proper PSCredential object. Function Fct_ConnectToAD { $User = "$erviceAccount@$ad" $PasswordFile = "$basefolder\Toolbox\auth.pxt" #$MyCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $PasswordFile | ConvertTo-SecureString -Key (Get-Content $KeyFile)) $MyCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $PasswordFile | ConvertTo-SecureString -Key (Get-Content $KeyFile)) return $MyCredential } #ConnectToAD I use another function Fct_MapPSDrive using the said PScredentials to create a mapdrive to my sharedfolder. function Fct_MapPSDrive { param( [Parameter (Mandatory = $true)] [string] $path, [Parameter (Mandatory = $true)] [string] $name, [Parameter (Mandatory = $true)] [System.Management.Automation.PSCredential] $credential ) New-PSDrive -Name $Name -Root $path -PSProvider "FileSystem" -Credential $Credential -Verbose } [System.Management.Automation.PSCredential] $mycredential = Fct_ConnectToAD Fct_MapPSDrive -path $PSDrivePath -Name $PSDriveName -credential $mycredential It works fine when I am inside my function. But once I get out of it I don't see the drive anymore using get-psdrive. Whether I run the script with my standard account or my service account doesn't make any difference. And Idea on how to perform this? If I can't use the drive then I would have to use the -credential on any single command of all my scripts (ex : new-item -credential $mycredential ...) which is not really a comfortable option... Thank you.Solved561Views0likes1CommentRetrieve executed script information the best way
Hello, I have this piece of code at the begining of most of my script. $ScriptFolderPath = $PSSCRIPTROOT $ScriptFolderName = (get-item $ScriptFolderPath ).Name $BaseFolder = (get-item $ScriptFolderPath ).parent.FullName $ScriptName = $MyInvocation.MyCommand.Name $ScriptNameNoExtension = [System.IO.Path]::GetFileNameWithoutExtension($MyInvocation.MyCommand.Source) Is there a clean, durable and standardized way of getting all these values (working in PS5 and 7) ? Thank you.1.8KViews0likes9Commentsexit powershell scriptS when imbricated
Hello, I have a script main.ps1 called with parameters "-environment PRD" or "-environment "PILOT". This main script calls a toolbox.ps1 script which is like this : switch ($environment) { "PILOT" { #define PILOT vars } "PRD" { #define PRD vars } default { write-host "!!! No Environment provided !! Closing the script." exit } } This exit seems to only close the toolbox.ps1 script but not the main.ps1 How can I achieve this ? Thank you.887Views0likes2Commentsreplace robocopy options with copy-item
Hello, I want to get rid of robocopy in some old scripts and use powershell copy-item instead. Here is a typical line : .\robocopy.exe "\\$sourceserver\$sourcefolder" "\\$targetserver\$targetfolder" *.* /r:2 /w:5 /e /purge /purge ask to remove anything at the target that doesn't exists at source. Basically it does a folder sync more than a source folder content copy to the target. /e copies also empty folders /r:2 retries twice if copy errors while /w:5 asks to wait 5 second between every try I mostly concerned with the /purge option. I don't see any option in copy-item that would do a sync of the source and target folder. Do I need to create a function to do it myself or is there any native way of doing it that I'm not aware of? This page suggest that it is not handled by default (see the very last proposal) : How to keep 2 folders in sync using powershell script - Stack Overflow Thank you.7KViews0likes1CommentUse to upper in pipe
Hello, I'd like to get all my "text" in uppercase right before sending it to CSV. Is this possible and how? (I don't want to do it anytime earlier in my script). I know the "string".ToUpper() method but no idea how to use something similar here. $hash.GetEnumerator() | Sort-Object $_.keys | ForEach-Object{[PSCustomObject]$_.value } | Sort-Object TimeID -Descending | Export-Csv "$hash" -NoTypeInformation -delimiter ";" Thank you.Solved1.3KViews0likes4CommentsPowershell : interprete string as variable name
Hello, I want to importe values from a csv file. Some column have this sort of values : $myvar. So it would be like this : log_message;server;scriptfolder;scriptname;action blabla;myserver;$scriptfolder;$scriptname;letsdance Now let say I want to do something like this : $scriptname = script.ps1 $references = import-csv .\mycsv -delimiter ";" foreach ($ref in $references){ write-host "$($ref.log_message) - $($ref.scriptname) } If I run something like this I get : blabla - $scriptname Is it possible to ask powershell to interprete the value of $scriptname instead of sending the string value $scriptname ? I tried get-variable($reference.scriptname) but I get this get-variable : Cannot find a variable with the name '$scriptname'. though write-host $scriptname script.ps1 Thank you.1KViews0likes1Commentstring not properly interpreted when calling a script
Hello, in a script I have to call psexec to execute a remote script on a remote target (I could probably do an invoke... but I didn't manage to make this work yet so for the moment I need to do it this way). ... .\psexec.exe "\\$targetserver" -accepteula powershell '"$pathtomyscript"'+$local_rep+'\'+$filename+'" "C:\_Scripts\_Logs"' ... When I run this no variable is interpreted. You can notice than the parameters block of the call to powershell is encapsulated in single quotes ' ' . You can also notice I tried to insert the variables in two different ways. If I simply select this : '"$pathtomyscript "view-pool-gbl" "noVM" "noref" "'+$local_rep+'\'+$filename+'" "C:\_Scripts\_Logs"' then powershell interpretes it properly and resolvs the variables, but as soon as it's part of the call to psexec then it's not interpreted. I have tried many other ways such as $var1+"text" etc but nothing seems to work. Does anyone know how to make this work ? Thank you. OSolved898Views0likes2Comments
Groups
Recent Blog Articles
No content to show