Loading...

These are all base cmdlets in PoSH (Host / ISE / VSCode).
If you are not able to run them, then you are not in a PoSH environment.

See if the below gets you to where you need to be.

Run Windows PowerShell Steps in SQL Server Agent
https://docs.microsoft.com/en-us/sql/powershell/run-windows-powershell-steps-in-sql-server-agent?view=sql-server-2017

Running PowerShell in a SQL Agent Job

Read Full Article
Visit website
  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 

hey js,

yeah thanks for your approach.
i will give direct path to filter to my requirement.

Read Full Article
Visit website
  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 

hi mike,
Thanks for your reply on this post.

i just wanted to exclude those mentioned 3 files and also i wanted to delete only "connect.12.1.nukpg" (file name with some no. with nupkg extension), may be the approach is same instead of excluding those 3 files, i think we can directly include Connect related files and delete them. but still i need the script in that way as i mentioned, i was trying to do that, i didn't get exact script.
In that folder or path there will be lot many files with different extension and filenames will starts with different names.

i hope you got my point !! please help me on this.

Read Full Article
Visit website
  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 

I am trying to create a SQL Server agent job that will run powershell commands in SQL Server 2008 R2. I keep getting errors. A lot of powershell commands it does not recognize (i.e. Import-module, Get-module, Invoke-cmd, etc). This is the error message I get "The term 'get-module' is not recognized as the name of a cmdlet, function, script file, or operable program." It works fine in the powershell ISE console. Any ideas on my problem?

Read Full Article
Visit website
  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 

In powershell, if there's no * in the path (or no -recurse), using both -exclude and -filter will prevent any files from being listed. I consider this a bug. It still exists in PS 6. Beginners often catch these things. -include will have the same effect as -filter.

PS C:\Users\js> ls


    Directory: C:\Users\js


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        5/24/2018   4:48 PM              4 file.jpg
-a----        5/24/2018   4:43 PM              4 file.pdf
-a----        5/24/2018   4:42 PM              4 file.txt


PS C:\Users\js> Get-ChildItem -Path . -Filter *.txt -Exclude *.pdf
PS C:\Users\js> Get-ChildItem -Path .\* -Filter *.txt -Exclude *.pdf


    Directory: C:\Users\js


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        5/24/2018   4:42 PM              4 file.txt
Read Full Article
Visit website
  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 

Jon,
Thanks for pointing that. So, I believe the discoverability of commands with required parameters looks quite hard then – IMO (For starters atleast). Ideally, I should import all modules (most often used) into the session and do the search- I guess

Read Full Article
Visit website
  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 

Thanks for sharing this snippet.It is so useful for new-comers like me.

Read Full Article
Visit website
  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 

The code you have *should* remove any explicitly defined ACEs that have principals that can't be resolved. Note that just because the computer you're currently on can't resolve a principal's SID to a user or group object doesn't necessarily mean that it's orphaned. True orhpaned ACEs do exhibit the same behavior, though, so I don't have the perfect answer for detecting truly orphaned ACEs. I'd just recommend making a backup of any ACEs before you remove them just in case.

Back to your specific problem: is there any chance the ACEs you can't remove are inherited? You should be able to tell that by looking at the $acc.IsInherited property. If it's inherited, you can remove it (you have to go higher up and remove it from the parent that has it explicitly defined).

Next, you said Get-Acl doesn't show it when you pipe it to Out-GridView? Do you mean you can see it from the terminal, but not in the OGV window? Or do you mean you can't see it in the terminal, either, but only see it in the Explorer GUI?

Finally, while your code looks like it should work, a few suggestions:
1. Set-Acl only needs to be called once for each folder. The whole security descriptor should be stored in the $acl variable, and you can call RemoveAccessRule() (and any other ACL modification method) multiple times, then call Set-Acl once when you're done. So I'd move Set-Acl outside of the foreach() block (it shouldn't hurt anything the way it's done, but it would save some time if you try to do this on a lot of folders at once if you move it)
2. Mike already mentioned this, but $folder is an object and should have access to the entire path of the folder. Try $folder.FullName instead of "$($Path)\$($folder.Name)
3. This code could remove non orphaned ACEs if you ever had a user whose name started with 'S-1-5-'. That almost definitely wouldn't happen, but it might be cleaner to test the $acc.IdentityReference type instead of the value of it's string contents. Either of these might work (I say might because I haven't tested them):

if ($acc.IdentityReference -is [System.Security.Principal.SecurityIdentifier]) {
    # It should have been turned into an [NtAccount] object if it could have been, so
    # this might be an orphaned ACE
}

## or ##

$CantTranslate = try {
    $NtAccount = $acc.IdentityReference.Translate([System.Security.Principal.NTAccount])
    $false
}
catch {
    $true
}
if ($CantTranslate) {
    # This computer couldn't translate the principal, so it might be orphaned
}
Read Full Article
Visit website
  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 

I don't have any orphan SIDs to test, but I think if you change your conditional operator -match to -like you might have better results. -Match is for regex and -like is for wildcards. I don't think "S-1-5-*" would be the regular expression for what you are looking for. Also on line 5 and 11 of your script if you use the FullName property you wouldn't have to do the "$($Path)\$($folder.Name)"

Read Full Article
Visit website
  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 

I'm not sure what you mean by "it must only delete those files whose name starts with connect." None of the exclusions you list start with "connect", so do you want to exclude "Evo.shibb*", "Hobsons.Platform*", "Shibboleth*" or only delete "connect*". Either way the approach is the same. Use get-childitem with the -exclude or -include switch depending on what you want to do, then pipe it to where-object and filter for only files older than 30 days, finally pipe that to remove-item. I would first use -whatif switch on remove-item before -force so you can be sure it is working as expected and not removing files you don't want to. The -whatif will only output text telling you what would happen if you ran the command. Here is an example:

$Excludes = "Evo.shibb*", "Hobsons.Platform*", "Shibboleth*"
Get-ChildItem -Exclude $Excludes | ? -Property CreationTime -LT ((Get-Date).AddDays(-30)) | Remove-Item -WhatIf
Read Full Article
Visit website

Read for later

Articles marked as Favorite are saved for later viewing.
close
  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 

Separate tags by commas
To access this feature, please upgrade your account.
Start your free month
Free Preview