How to create a custom event view in powershell? - powershell

I've been looking around and can't find any way to create a custom view in the event viewer in Powershell.
I found a Microsoft Scripting Guy post about exporting one, but I'd like to create one from scratch. I'm using PowerShell 5.0.
Edit:
Looks like all the event logs are stored in %SystemRoot%\System32\Winevt\Logs\ and exist as .evtx files.
Edit:
The command:
New-EventLog -LogName "LogNameHere" -Source "Test"
does create LogNameHere.evtx in the %SystemRoot%\System32\Winevt\Logs\ directory. You can specify multiple sources, but this just allows those sources to write to the event log, and doesn't log all the information from those sources. (I'll check on that)
New-EventLog -LogName "LogNameHere" -Source "Test1", "Test2", "Test3"

Related

Moving Document Library to a subsite in SharePoint

Currently, we have Document Libraries created in SharePoint Online and would like to move them using Powershell to its own Subsite. The reason we would like to move them is that we would like to keep the version history. Since we are dealing with 1000s of files, I would like to use Powershell to complete this task.
I am currently connecting to my SharePoint site using:
Connect-PnPOnline -Url "Sitename" -UseWebLogin
Here is where I need assistance. I am trying to use Move-PnPFolder but I am not sure how to write a command that would define the source, destination, and move of all files in the document library to a subsite that I have manually created.
Here you need to mix of PnP and CSOM PS script... the normal way how we get the SharePoint list items... we need read the the list items then inside the foreach loop you need to call the below command : Move-PnPFolder -Folder 'Shared Documents/Reports/2016/Templates' -TargetFolder 'Shared Documents/Reports' Something like below :
foreach ($oneFolder in List. Folders) { Move-PnPFolder -Folder 'Shared Documents/Reports/2016/Templates' -TargetFolder 'Shared Documents/Reports' }
Note :
In the above command from the $oneFolder property you will get the source folder URL and you already know the target folder location.
This is just sample code to present the concept.

Is there a way to find which user run what application on a server using Powershell

I am trying to find a way to find out who has ran an application (for example SQL) on a server, just to get some idea.
I tried Get-Process but this doesn't give me historic information, I want to get historical information
Get-Process -IncludeUserName *
what I want the return resule is "name of application", "user who ran it" and the last datetime it was ran by that user'
As for ...
I am trying to find a way to find out who has ran an application (for
example SQL) on a server, just to get some idea.
What you are asking for here is software metering.
SQL is a service that is always running once it is installed, so, no individual user is ever going to be running it. So, that is a bad example. MS Word for example would be a better example.
Yet there is nothing native in PowerShell that does this, software metering, but of course PowerShell can look at event logs. Yet if your auditing is not setup correctly then it's moot. This is better for a software metering tool, and there are several out there. So, why try and reinvent the wheel.
As for ...
I tried Get-Process but this doesn't give me historic information, I
want to get historical information
That is not what a process is nor what Get-Process is for. It, Get-Process only checks for and lists whatever process is currently running, regardless of what/who launched it.
As for...
what I want the return resule is "name of application", "user who ran
it" and the last datetime it was ran by that user'
As long as the process is running, you can get this, with that cmdlet.
However, what are you trying to accomplish by this?
Again, there are purpose built tools to meter software use.
https://learn.microsoft.com/en-us/sccm/apps/deploy-use/monitor-app-usage-with-software-metering
If you must go down this reinvent the wheel road, using scripting, then you need a task watcher on the target machines, which watches for the WinWord process to appear.
Get-Process -IncludeUserName |
Where ProcessName -EQ 'Winword'
... you then write those results to a file or database or your own event log each time you see that process.
Use PowerShell to Create and to Use a New Event Log
New-EventLog -LogName ScriptingGuys -Source scripts
When the command runs, no output appears to the Windows PowerShell console. To ensure the command actually created a new event log, I use
the Get-EventLog cmdlet with the –List parameter. Here is the command
and the associated output.
Write-EventLog -LogName ScriptingGuys -Source scripts -Message “Dude, it works … COOL!” -EventId 0 -EntryType information
Or just to a file
Get-Process -IncludeUserName |
Where ProcessName -EQ 'Winword' |
Select-Object -Property Name, StartTime, Username |
Export-Csv -Path 'F:\Temp\AppLaunchLog.csv' -Append
Import-Csv -Path 'F:\Temp\AppLaunchLog.csv'
# Results
Name StartTime UserName
---- --------- --------
WINWORD 5/23/2019 9:02:53 PM WS01\LabUser001

Sitecore download media folder with powershell

I have a requirement to remove several PDFs from specific folder our Sitecore media library, possibly over 1000 documents. We need to maintain the file names of these PDFs and would prefer to maintain the "file structure/hierarchy" of these documents.
We have tried using the Sitecore Powershell Extensions "Download" feature, but it does not appear to download the PDFs, only the content items themselves. We can create a package, but the folder that contains the resulting PDFs have had their file names replaced with GUIDs.
Is there a PS script available or any other way to do a bulk download of these media library PDFs?
P.S. I am a Powershell neophyte, so please be gentle.
Sounds like you need to use the Send-File command. When executed on a media library item, it allows you to download the associated blob stored in that item.
Example:
PS master:\>Get-Item "master:\media library\files\pdfs\mypdffile" | Send-File -Message "PDF Download"
You can pass -NoDialog to it so the user is not propmted each time.
So for multiple files you can do:
Get-Item -Path master -Query "/sitecore/#media library#/files//*[##templatename='Pdf']" |
Send-File -NoDialog
I can't see performance being great with 1000's of files for that tho. Also it will not keep the path, all files will be saved directly to your Downloads folder.
Perhaps you may be interested in using the download capability through SPE Remoting. This is an alternative solution that operates outside of Sitecore and connects through the web service.
Sample script for downloading:
https://github.com/SitecorePowerShell/Console/blob/master/Modules/Remoting%20Tests%20-%20Download%20with%20RemoteScriptCall.ps1
Book details on getting setup:
https://sitecorepowershell.gitbooks.io/sitecore-powershell-extensions/content/remoting.html

Sitecore powershell script - how to initialize custom scripts

I used sitecore powershell last year to migration heaps of items.
I installed the latest one and I don't see "Initialization script" field under Console/All Users item.
I had used this field to intialize all my custom scripts like below so that I can access from PSE.
Execute-Script -Item (Get-Item -ID "{0B0E50B9-CD3C-4FE7-BB6D-D2A9AAEB7568}" -Path master:\)
Any help will be good.
This was removed in SPE 3.1 due to the unpredictable nature.
https://github.com/SitecorePowerShell/Console/issues/365
If you could post a new issue on what your custom script does, perhaps we can provide you with an alternate solution.

How should I create a reusable "log" object in Powershell?

I'm new to PowerShell and am writing my first application. The application will check the state of a Windows service and (depending on a number of factors) can do a number of things, such as stop it, start it or restart it. I need to produce a log showing what it's done. I'm wanting to use modules so my code will be reusable and also flexible (for example, if I schedule it to run automatically, the log will need to be sent by e-mail or written to a text file, if I run it manually, I will want it outputting with something like Write-Host).
My question is, how do I create the log? In Java for example, I would use a log class with public void addToLog(String log) and public String getLog() methods which just deal with strings and leave it up to e-mail/display it etc.
I have a quite complex set of scripts for installing our platform (relying on BizTalk, SQL Server, Enterprise Single Sign-on, IIS, Enterprise Library and a few other things). These scripts start and stop services, BizTalk orchestrations, create or update databases and so on. I tried several things for logging and, finally, I picked log4net for its ease of use and flexibility. Using it from PowerShell is a breeze.
Refer to a similar question which I had asked on SO few days ago. Might help. I am using the same Logging Module referred there in the ANSWER - Powershell: Debug in Production / Good Exception Handling
If you need a log for people who are in charge of the production you can add your own application log using dedicated Cmdlets. A the moment I create and use one PowerShell log for all my scripts to publish details of the execution (information) and errors (in coordination with good exception handling) for people who are in charge of the production. You can dedicate a log for one script (as it exists a log for DNS etc.)
Here is an example :
# List of logs
Get-EventLog -list
# Creating your own log
New-EventLog -LogName "SlxScripting" -Source "MaSource"
# List of logs
Get-EventLog -list
# Writting in your own log
Write-EventLog -LogName "SlxScripting" -EventId 12 `
-Message "Mon Message"
-Source "MaSource" -EntryType Warning
# Reading in your own log
Get-EventLog -LogName "SlxScripting"
# Suppressing your log
Remove-EventLog -LogName "SlxScripting"