1) Here's my schema:
{
"_id" : ObjectId("53f4db1d968166157c2d57ce"),
"init" : "SJ",
"name" : "Steve Jobs",
"companies" : [
{
"_id" : ObjectId("53f4db1d968166157c2d57cf"),
"ticker" : "AAPL",
"compname" : "Apple"
},
{
"_id" : ObjectId("53f4db1d968166157c2d57d0"),
"ticker" : "MSFT",
"compname" : "Microsoft"
},
{
"_id" : ObjectId("53f4db1d968166157c2d57d1"),
"ticker" : "ABC",
"compname" : "iTunes"
},
{
"_id" : ObjectId("53f4db1d968166157c2d57d2"),
"ticker" : "DEF",
"compname" : "iPad Mini"
}
]
}
I'm trying to get a list of compnames, using Powershell & MongoDB. Here's what I have so far:
$databaseName = "CompanyInfo"
$collectionName = "comps"
$client = New-Object -TypeName MongoDB.Driver.MongoClient -ArgumentList "mongodb://localhost:27017"
$server = $client.GetServer()
$database = $server.GetDatabase($databaseName)
$collection = $database.GetCollection($collectionName)
$query['init'] = "SJ"
$results = $collection.FindOne($query)
foreach ($result in $results) {
write-host $result["companies.ticker"] /// Doesn't show me any records
}
This doesn't show me any records. How can I display companies.ticker info where init = "SJ"?
2) Btw, I get the following error after
$query['init'] = "SJ"
error
Cannot index into a null array.
At line:9 char:1
+ $query['init'] = "SJ"
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
Any ideas as to why? I only have the MongoDB's standard index, which is on "_id", nothing else. My powershell script still works but I'm curious as to why I get that error.
[UPDATE Part 2] Thanks to #arco444, I no longer get error in part 2. Here's my revised code:
$query = #{'init' = "SJ"}
$collection.FindOne([MongoDB.Driver.QueryDocument]$query)
But I actually need help with part 1 - which is to display only the company tickers for a particular init. Any ideas on that one?
[ANSWER Part 1] Thanks again to #arco444 for directing me to the right path. After some tinkering around, I figured out what I missed. Here's my updated code:
$databaseName = "CompanyInfo"
$collectionName = "comps"
$client = New-Object -TypeName MongoDB.Driver.MongoClient -ArgumentList "mongodb://localhost:27017"
$server = $client.GetServer()
$database = $server.GetDatabase($databaseName)
$collection = $database.GetCollection($collectionName)
$query = new-object MongoDB.Driver.QueryDocument("init","SJ") /// Updated
$results = $collection.FindOne($query)
foreach ($result in $results["companies"]) { /// Updated
write-host $result["ticker"] /// Updated
}
From reading the MongoDB documentation, it looks like you need to initialise the query object properly first. Try this:
$query = new-object MongoDB.Driver.QueryDocument("init","SJ")
$results = $collection.FindOne($query)
Here's my updated code:
$databaseName = "CompanyInfo"
$collectionName = "comps"
$client = New-Object -TypeName MongoDB.Driver.MongoClient -ArgumentList "mongodb://localhost:27017"
$server = $client.GetServer()
$database = $server.GetDatabase($databaseName)
$collection = $database.GetCollection($collectionName)
$query = new-object MongoDB.Driver.QueryDocument("init","SJ") /// Updated
$results = $collection.FindOne($query)
foreach ($result in $results["companies"]) { /// Updated
write-host $result["ticker"] /// Updated
}
So when I use your query procedure with
$mongoDbDriverPath = 'D:\mongo\driver\'
$mongoServer = 'myserver:27000'
Add-Type -Path "$($mongoDbDriverPath)MongoDB.Bson.dll"
Add-Type -Path "$($mongoDbDriverPath)MongoDB.Driver.dll"
$databaseName = 'Tickets'
$collectionName = 'MongoUserTicket'
$client = New-Object -TypeName MongoDB.Driver.MongoClient -ArgumentList "mongodb://$mongoServer"
$server = $client.GetServer()
$database = $server.GetDatabase($databaseName)
$collection = $database.GetCollection($collectionName)
$query = new-object Mongodb.driver.querydocument('Id','5')
$query2 = New-object Mongodb.driver.querydocument('Type','User')
$results = #()
foreach($item in $collection.Find($query))
{
$results += $item
}
I get unexpected results for the query:
When piping to Get member for results I get this:
TypeName: MongoDB.Bson.BsonElement
Name MemberType Definition
---- ---------- ----------
Clone Method MongoDB.Bson.BsonElement Clone()
CompareTo Method int CompareTo(MongoDB.Bson.BsonElement other), int IComparable[BsonElement].CompareTo(MongoDB.Bson.BsonElement other)
DeepClone Method MongoDB.Bson.BsonElement DeepClone()
Equals Method bool Equals(MongoDB.Bson.BsonElement rhs), bool Equals(System.Object obj), bool IEquatable[BsonElement].Equals(MongoDB.Bson.BsonElement other)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
Name Property string Name {get;}
Value Property MongoDB.Bson.BsonValue Value {get;set;}
When entering $results on the shell prompt I get the data I expect:
$results
Name Value
---- -----
Id 5
AccessToken
CreatedOn 2013-09-27T22:05:52.246Z
TokenExpiration 2013-09-27T22:20:52.246Z
RefreshTokenExpiration 2013-09-28T22:05:52.246Z
ProfileToken BsonNull
Type User
Id 5
AccessToken
CreatedOn 2013-09-27T23:42:28.492Z
TokenExpiration 2013-09-27T23:57:28.492Z
RefreshTokenExpiration 2013-09-28T22:06:04.071Z
ProfileToken BsonNull
Type User
here is what allowed me to get an object that i could operate on:
$results = #()
foreach($item in $collection.Find($query))
{
$props = #{}
$item | foreach { $props[ $_.name ] = $_.value }
$pso = [pscustomobject]$props
$results += $pso
}
full code:
$mongoDbDriverPath = 'D:\mongo\driver\'
$mongoServer = 'myserver:27000'
Add-Type -Path "$($mongoDbDriverPath)MongoDB.Bson.dll"
Add-Type -Path "$($mongoDbDriverPath)MongoDB.Driver.dll"
$databaseName = 'Tickets'
$collectionName = 'MongoUserTicket'
$client = New-Object -TypeName MongoDB.Driver.MongoClient -ArgumentList "mongodb://$mongoServer"
$server = $client.GetServer()
$database = $server.GetDatabase($databaseName)
$collection = $database.GetCollection($collectionName)
$query = new-object Mongodb.driver.querydocument('Id','5')
$query2 = New-object Mongodb.driver.querydocument('Type','User')
$results = #()
foreach($item in $collection.Find($query))
{
$props = #{}
$item | foreach { $props[ $_.name ] = $_.value }
$pso = [pscustomobject]$props
$results += $pso
}
$results
Related
Can someone help me and point out why my script isn't looping through my server list and not inserting all the data to my sql table?
$SQLserverDB = #()
$SQLserverDB = get-content "D:\Temp\servers.txt"
foreach ($SQLserver in $SQLserverDB) {
$server = New-Object Microsoft.sqlserver.management.smo.server($SQLserver)
Write-Host "--Server List:"$server"---" -ForegroundColor Yellow
foreach ($db in $Server.Databases) {
$dbobj = New-Object PSObject -Property ([ordered]#{
Instance_Name = $db.Parent
Database_Name = $db.Name
Database_ID = $db.id
Active = $db.Status
DateStamp = $db.CreateDate
Owner = $db.Owner
Compatibility_Level = $db.compatibilitylevel
Recovery_Model = $db.recoverymodel
DB_Size_MB = [Math]::round($db.size, 2)
}
)
}
}
$dt = Out-DbaDataTable -InputObject $dbobj
Write-SqlTableData -ServerInstance localhost -DatabaseName DBA_Central -SchemaName dbo -TableName database_masters2 -InputData $dt
My approach is to get the latest ModifyTimeStamp after scanning on all DC's. The scenario in my code is:
First, I scan on the PDC to get the distinguishedName values, and after that I scan on all DC's also to get distinguishedName values, if they are -eq to each other, it will print the ModifyTimeStamp which means all ModifyTimeStamp values on each DC's will be stored in an arraylist. The arraylist will print the maximum values then on. As the following:
$TrustedDomain = "test.com"
$context = new-object System.DirectoryServices.ActiveDirectory.DirectoryContext("domain",$TrustedDomain)
$D = [System.DirectoryServices.ActiveDirectory.Domain]::GetDomain($context)
$PDC = $D.PdcRoleOwner
$ADSearch = New-Object System.DirectoryServices.DirectorySearcher
$ADSearch.SearchRoot ="LDAP://$PDC"
$ADSearch.SearchScope = "subtree"
$ADSearch.PageSize = 100
$ADSearch.Filter = "(&(objectCategory=person)(objectClass=user))"
foreach($pro in $properies)
{
$ADSearch.PropertiesToLoad.add($pro)| out-null
}
$userObjects = $ADSearch.FindAll()
$dnarr = New-Object System.Collections.ArrayList
Function modiScan{
$Searcher = New-Object System.DirectoryServices.DirectorySearcher
$Searcher.PageSize = 100
$Searcher.SearchScope = "subtree"
$Searcher.Filter = "(&(objectCategory=person)(objectClass=user))"
$Searcher.PropertiesToLoad.Add("distinguishedName")|Out-Null
$Searcher.PropertiesToLoad.Add("modifyTimeStamp")|Out-Null
forEach ($users In $userObjects)
{
$DN = $users.Properties.Item("distinguishedName")[0]
$dnarr.add($DN)|Out-Null
}
#$dnarr
foreach($dnn in $dnarr){
$lastmd = New-Object System.Collections.ArrayList
ForEach ($DC In $D.DomainControllers){
$Server = $DC.Name
$Base = "LDAP://$Server/"+$dnn
$Searcher.SearchRoot = $Base
$Results2 = $Searcher.FindAll()
ForEach ($Result2 In $Results2)
{
$DN2 = $Result2.Properties.Item("distinguishedName")[0]
if($DN2 -eq $dnn){
$modi = $Result2.Properties.Item("modifyTimeStamp")[0]
$lastmd.Add($modi)|Out-Null
}
}
}
$lastModi = ($lastmd |measure -max).maximum
if($lastModi -ne $null){
$lastModi = $lastModi.ToString("yyyy/MM/dd")
}
else{
$lastModi = "N/A"
}
$lastModi
}
}
modiScan
The error I've got is:
Exception calling "FindAll" with "0" argument(s): "Unknown error (0x80005000)"
At C:\Users\Ender\trustedScan.ps1:40 char:21
+ $Results2 = $Searcher.FindAll()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : COMException
I have executed on current Domain it worked like a charm. But when I try to put a trusted domain, it throws me that error.
This has no issue with SQL. it's when creating the rtf object.
I am connecting to a sql database and pulling information. Some of the information is html,rtf, and plain text. After about 10 mins of running I get this:
Exception setting "Rtf": "Error creating window handle."
At line:24 char:76
+ ... Name System.Windows.Forms.RichTextBox; $rtf.Rtf = $convo.Body; $body ...
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
+ FullyQualifiedErrorId : ExceptionWhenSetting
Has anyone else ran into this issue?
Here is the script itself.
#Who are you searching for?
#Example User ID: user#domain.com
$Subject = "changeme#domain.com"
#Set the date to search from
#Example date format: 2016-08-16.
#Leave it blank if you don't want to search for just dates.
$Date = ""
#Blank array to store the conversation history
$arr = #()
#Lync Archive Server
$SQLSvr = "ServerName Goes Here"
#Lync Archive Database
$Database = "LcsLog"
#Get the UserId's
$UserUri = Invoke-Sqlcmd -Query "Select UserUri,UserId From dbo.Users u;" -ServerInstance $SQLSvr -Database $Database
#Build the Select Statement
$select = "Select * from dbo.Users d left join dbo.Messages m on FromId = d.UserId or ToId = d.UserId Where d.UserUri = '$Subject' "
if($Date)
{
$select = $select +"and m.MessageIdTime >= '$Date 00:00:01.550' order by m.MessageIdTime asc;"
}
else
{
$select = $select + "order by m.MessageIdTime asc;"
}
#Get the conversation history
$ConvoData = Invoke-Sqlcmd -Query ($select) -ServerInstance $SQLSvr -Database $Database;
#Loop through each conversation
foreach($convo in $ConvoData)
{
#Loop through each user.
foreach($user in $UserUri)
{
#Verify the FromId
if($convo.FromId -eq $user.UserId)
{
$FromID = $user.UserUri
}
#Verify the ToId
if($convo.ToId -eq $user.UserId)
{
$ToId = $user.UserUri
}
}
#Parse the body for legible reading
switch ($convo.ContentTypeId)
{
'1' {$html = New-Object -ComObject "HTMLFile"; $html.IHTMLDocument2_write($convo.Body);$body = $html.IHTMLDocument2_body.innerText; $html.close();}
'2' {$rtf = New-Object -TypeName System.Windows.Forms.RichTextBox; $rtf.Rtf = $convo.Body; $body = $rtf.Text; $rtf.Clear();}
'3' {$body = $convo.Body}
}
#Build the Message Output
$obj = New-Object -TypeName psobject -Property #{User = $Subject; "Message Time" = $convo.MessageIdTime; From = $FromID; To = $ToId; Body = $body}
#Add data to the array
$arr += $obj
}
$arr | Select User,"Message Time",From,To,Body | Export-csv "$env:userprofile\desktop\$Subject - conversation report.csv"
Not really an answer, but a recommendation that you should turn your parameters into a Param block. It would be more useful if you wanted to call the script from the command line or turn it into a function.
Param (
# Parameter Subject
[Parameter(Mandatory = $true,
HelpMessage = 'Who are you searching for? e.g. User ID: user#domain.com')]
$Subject = 'changeme#domain.com',
# Parameter Date
[Parameter(HelpMessage = 'Set the date to search from. e.g. "2016-08-16"')]
[String]
$Date,
# Parameter SQLSvr
[Parameter(Mandatory = $true,
HelpMessage = 'ServerName Goes Here')]
$SQLSvr,
# Parameter Database
[Parameter(Mandatory = $true,
HelpMessage = 'Lync Archive Database')]
$Database = 'LcsLog'
)
I figured it out. By creating one instance of my RTF object at the beginning it corrected my error.
#Who are you searching for?
#Example User ID: user#domain.com
$Subject = "changeme#domain.com"
#Set the date to search from
#Example date format: 2016-08-16.
#Leave it blank if you don't want to search for just dates.
$Date = ""
#Blank array to store the conversation history
$arr = #()
#Create RTF and HTML Objects
$html = New-Object -ComObject "HTMLFile";
$rtf = New-Object -TypeName System.Windows.Forms.RichTextBox;
#Lync Archive Server
$SQLSvr = "Server Name goes here"
#Lync Archive Database
$Database = "LcsLog"
#Get the UserId's
$UserUri = Invoke-Sqlcmd -Query "Select UserUri,UserId From dbo.Users u;" -ServerInstance $SQLSvr -Database $Database
#Build the Select Statement
$select = "Select * from dbo.Users d left join dbo.Messages m on FromId = d.UserId or ToId = d.UserId Where d.UserUri = '$Subject' "
if($Date)
{
$select = $select +"and m.MessageIdTime >= '$Date 00:00:01.550' order by m.MessageIdTime asc;"
}
else
{
$select = $select + "order by m.MessageIdTime asc;"
}
#Get the conversation history
$ConvoData = Invoke-Sqlcmd -Query ($select) -ServerInstance $SQLSvr -Database $Database;
#Loop through each conversation
foreach($convo in $ConvoData)
{
#Loop through each user.
foreach($user in $UserUri)
{
#Verify the FromId
if($convo.FromId -eq $user.UserId)
{
$FromID = $user.UserUri
}
#Verify the ToId
if($convo.ToId -eq $user.UserId)
{
$ToId = $user.UserUri
}
}
#Parse the body for legible reading
switch ($convo.ContentTypeId)
{
'1' {$html.IHTMLDocument2_write($convo.Body);$body = $html.IHTMLDocument2_body.innerText; $html.close();}
'2' {$rtf.Rtf = $convo.Body; $body = $rtf.Text; $rtf.Clear();}
'3' {$body = $convo.Body}
}
#Build the Message Output
$obj = New-Object -TypeName psobject -Property #{User = $Subject; "Message Time" = $convo.MessageIdTime; From = $FromID; To = $ToId; Body = $body}
#Add data to the array
$arr += $obj
}
$arr | Select User,"Message Time",From,To,Body | Export-csv "$env:userprofile\desktop\$Subject - conversation report.csv"
I have a script which I think is most of the way to being able to grab IE history for all of the users on a machine. My problem is it doesn't work for users other than myself because I get a permissions error when attempting open the folder. Does anyone have any ideas how I can fix the permissions problem?
I'm running this script on a Windows 2012r2 machine and I am an administrator on the box.
Thanks!
function Get-History
{
param
(
[string]$userName
)
$shell = New-Object -ComObject Shell.Application
if($username)
{
$users = $username
}
else
{
$users = Get-ChildItem C:\Users
}
if((([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]"Administrator")))
{
Foreach($user in $users)
{
$user = Split-Path $user -leaf
try
{
$ErrorActionPreference = 'Stop'
$hist = $shell.NameSpace("C:\Users\$user\AppData\Local\Microsoft\Windows\History")
}
catch
{
continue
}
$folder = $hist.Self
#$folder.Path
if($hist){
$hist.Items() | foreach {
#""; ""; $_.Name
if ($_.IsFolder) {
$siteFolder = $_.GetFolder
$siteFolder.Items() | foreach {
$site = $_
#""; $site.Name
if ($site.IsFolder) {
$pageFolder = $site.GetFolder
$pageFolder.Items() | foreach {
$url = $pageFolder.GetDetailsOf($_,0)
$date = $pageFolder.GetDetailsOf($_,2)
#"$user`: $date visited $url"
#Write-Output ("$user,$date,`"$url`"" | ConvertFrom-Csv)
New-Object -TypeName PSObject -Property #{
user=$user;
date = $date;
url = $url
}
}
}
}
}
}
}
}
}
else
{
Write-Host "Not Admin"
}
}
If I run just the small snippet:
$shell = New-Object -ComObject Shell.Application
$hist = $shell.NameSpace("C:\Users\MyOwnUsername\AppData\Local\Microsoft\Windows\History")
Then I successfully assign the $hist variable as a System.__ComObject
But if I run:
$shell = New-Object -ComObject Shell.Application
$hist = $shell.NameSpace("C:\Users\SomeOtherUser\AppData\Local\Microsoft\Windows\History")
I get:
Exception calling "NameSpace" with "1" argument(s): "Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))"
At line:3 char:1
+ $hist = $shell.NameSpace("C:\Users\SomeOtherUser\AppData\Local\Microsoft\Windows\History ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ComMethodTargetInvocation
Full Disclosure: it's a slight modification of the script at this blog post
I never solved the permissions problem. I changed strategy to use BrowsingHistoryViewer by Nirsoft.
$execPath = "C:\BrowsingHistoryView.exe"
$computers = 'computer1','computer2','computer3','computer4'
$outPath = "c:\"
$computers |
ForEach-Object{ `
Start-Process $execPath `
-argumentList "/HistorySource 3",
"/HistorySourceFolder ""\\$_\c$\Users""",
"/scomma ""$outpath\history_$_.txt"""
}
I am using the following to get the status of tracert
Currently it stores it in a New-Object psobject but the problem I am running into is that when I try and filter on Status wanting just to return Success I get the following returned instead #{Status=Success}, how can I remove the #{Status=} from around the results?
function Invoke-Trace() {
param(
[string[]]$targetIP,
$BeginHop = 1,
$EndHop = 30,
$timeout = 1000,
[switch]$GetHostname
)
$addrtype = [System.Net.Sockets.AddressFamily]::InterNetwork;
if($v6.ispresent) {
$addrtype = [System.Net.Sockets.AddressFamily]::InterNetworkV6;
}
$targetIPActual = $null;
if(![net.ipaddress]::TryParse($targetIP, [ref]$targetIPActual)) {
$target = [net.dns]::GetHostEntry($targetIP);
$targetIPActual = $target.addresslist | where {$_.addressfamily -eq $addrtype} | select -First 1
} else {
$target = New-Object psobject -Property #{"HostName" = $targetIP.tostring()}
}
for($i = $BeginHop; $i -lt $EndHop; $i++) {
$ping = new-object System.Net.NetworkInformation.ping;
$pingo = new-object System.Net.NetworkInformation.PingOptions $i, $true;
$sendbytes = #([byte][char]'a'..[byte][char]'z');
$pr = $ping.Send($targetIPActual, $timeout, $sendbytes, $pingo);
try {
$rtn = New-Object psobject -Property #{
"IP" = $pr.Address;
"RoundtripTime" = $pr.RoundtripTime;
"Status" = $pr.Status;
}
} catch {
$rtn = New-Object psobject -Property #{
"IP" = "*";
"RoundtripTime" = $pr.RoundtripTime;
"Status" = $pr.Status;
}
}
try {
if($GetHostname.ispresent) {
Add-Member -InputObject $rtn -MemberType NoteProperty -Name Hostname -Value ([net.dns]::GetHostEntry($pr.Address).hostname)
}
} catch{}
$rtn;
#$pr
try {
if($pr.Address.tostring() -eq $targetIPActual) { break; }
} catch{}
}
}
If your $rtn is a PSObject and you want to just return one property of it, then don't return the whole object. The line above #$pr is where your object is being returned, so you could do this instead:
$rtn.Status
It's a bit unclear to me why you're putting it in that object in the first place since you don't seem to want to use it, but I'm just going to assume you have a reason and give you this quick answer. Feel free to edit your question and clarify if there's something that might be missing.