

{"id":347,"date":"2016-07-22T13:57:30","date_gmt":"2016-07-22T03:57:30","guid":{"rendered":"https:\/\/kng.tech\/blog\/?p=347"},"modified":"2016-09-16T12:23:16","modified_gmt":"2016-09-16T02:23:16","slug":"works-on-excel-files-with-powershell","status":"publish","type":"post","link":"https:\/\/kng.tech\/blog\/works-on-excel-files-with-powershell\/","title":{"rendered":"Works on Excel files with Powershell"},"content":{"rendered":"<p>Here is a powershell script where it could \"compare and update\".<br \/>\nThis particular powershell will look for the username and insert it from one file to the other.<\/p>\n<p>* Adjust and use it in your own risk.<\/p>\n<pre><code>\r\n$strPath = \"C:\\s\\library.csv\"  # this is the main spreadsheet which we look for the username and update the password\r\n$strPath2 = \"C:\\s\\Year102016.csv\" # this is the csv file which contains the username and password\r\n\r\n$objExcel = New-Object -ComObject Excel.Application\r\n$objExcel.Visible = $false\r\n\r\n$workbook1 = $objExcel.Workbooks.Open($strPath)\r\n$worksheet1 = $workbook1.sheets.item(\"library\")\r\n$intRowMax1 =  ($worksheet1.UsedRange.Rows).count\r\n\r\n$workbook2 = $objExcel.Workbooks.Open($strPath2)\r\n$worksheet2 = $workbook2.sheets.item(\"Year102016\")\r\n$intRowMax2 =  ($worksheet2.UsedRange.Rows).count\r\n\r\n\r\n$colA = 1 # column contain the username\r\n$colO = 15 # Where we need to insert the password\r\n\r\n# \r\n#\r\nFunction checkAndAdd\r\n{\r\n\r\n        Param($username, $password)\r\n\r\n        write-host \"Second FOR Loop: \" $intRow1 \" Looking for username: \" $username\r\n\r\n        # Loop from Row 1 to the last Row\r\n        for ($intRow1 = 1 ; $intRow1 -le $intRowMax1 ; $intRow1++) \r\n        {\r\n            # read the username into $colA\r\n            $colA = $worksheet1.cells.item($intRow1,1).value2 \r\n\r\n            if ($colA -match $username)\r\n            {\r\n                Write-Host \"Found username: \" $username \" insert password to column now.\"\r\n                $worksheet1.cells.item($intRow1, $colO) = $password\r\n                break\r\n            } \r\n                \r\n         } # ROW FOR LOOP\r\n}\r\n\r\n\r\n\r\n# READ the Year102016.csv row by row, pass in the Username, Password, to the other Excel file\r\n# MAIN For Loop\r\n#\r\n\r\nFunction ReadUsernamePassword\r\n{ \r\n    # Loop from Row 2 to the last Row\r\n    for ($intRow2 = 2; $intRow2 -le $intRowMax2 ; $intRow2++) \r\n    {\r\n        # Make sure column 1 = username, column 8 = password\r\n        $username = $worksheet2.cells.item($intRow2, 1).value2\r\n        $password = $worksheet2.cells.item($intRow2, 8).value2\r\n\r\n        \r\n        write-host \"MAIN For Loop: \"$intRow2 \" checking username: \" $username \" password: \" $password \r\n        checkAndAdd $username $password\r\n\r\n    } \r\n}    \r\n#>\r\n\r\n\r\n# Check 1 code only\r\n# Beware the strPath file that you read\/write to may need to change.\r\n\r\nFunction Check1Code\r\n{\r\n    Param($username, $password)\r\n    checkAndAdd $username $password\r\n}\r\n\r\n## Uncomment this to Run Check1Code  \r\n## \r\n## \r\n#\r\n#Check1Code \"abc1234\" \"password\"\r\n\r\nReadUsernamePassword\r\n\r\n      \r\n$workbook1.SaveAs('C:\\s\\LibraryFINAL.csv') # output file \r\n$objexcel.quit()\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Here is a powershell script where it could \"compare and update\". This particular powershell will look for the username and insert it from one file to the other. * Adjust and use it in your own risk. $strPath = \"C:\\s\\library.csv\" # this is the main spreadsheet which we look for the username and update the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[43,27],"class_list":["post-347","post","type-post","status-publish","format-standard","hentry","category-tech","tag-powershell","tag-script"],"_links":{"self":[{"href":"https:\/\/kng.tech\/blog\/wp-json\/wp\/v2\/posts\/347","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kng.tech\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kng.tech\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kng.tech\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kng.tech\/blog\/wp-json\/wp\/v2\/comments?post=347"}],"version-history":[{"count":2,"href":"https:\/\/kng.tech\/blog\/wp-json\/wp\/v2\/posts\/347\/revisions"}],"predecessor-version":[{"id":422,"href":"https:\/\/kng.tech\/blog\/wp-json\/wp\/v2\/posts\/347\/revisions\/422"}],"wp:attachment":[{"href":"https:\/\/kng.tech\/blog\/wp-json\/wp\/v2\/media?parent=347"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kng.tech\/blog\/wp-json\/wp\/v2\/categories?post=347"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kng.tech\/blog\/wp-json\/wp\/v2\/tags?post=347"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}